Skip to content

Commit b2d8c3d

Browse files
authored
[alphaorderly] WEEK 03 Solutions (#2707)
* week 1 * [alphaorderly] WEEK 02 Solutions * fix: description์— ๋งž๊ฒŒ ์ฝ”๋“œ ์ˆ˜์ • * fix: ์ข€ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ˆ˜์ • * [alphaorderly] WEEK 03 Solutions * fix: ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ ์‚ญ์ œ * fix: ์ค„๋ฐ”๊ฟˆ ๋ฆฐํŠธ ๋ฌธ์ œ ์ˆ˜์ • * [alphaorderly] WEEK 03 Solutions * fix: ๋ฆฐํŠธ ์˜ค๋ฅ˜ ์ˆ˜์ • * fix: ํŒŒ์ด์ฌ ๋‚ด์žฅํ•จ์ˆ˜ ์‚ฌ์šฉ ์ฝ”๋“œ ์ถ”๊ฐ€ * fix: valid-palindrome ๋ฌธ์ œ์— ํˆฌํฌ์ธํ„ฐ ๊ตฌํ˜„ ๋‹ต์•ˆ ์ถ”๊ฐ€ * fix: ๋กœ์ง ๊ฐ€๋…์„ฑ ์ˆ˜์ •
1 parent 2cf8bdc commit b2d8c3d

5 files changed

Lines changed: 206 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
"""
3+
Time Complexity: O(N^target)
4+
Space Complexity: O(target)
5+
6+
Classic backtracking approach.
7+
- Use a helper function to backtrack and generate all possible combinations.
8+
"""
9+
# class Solution:
10+
# def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
11+
# N = len(candidates)
12+
# candidates.sort()
13+
14+
# ans = []
15+
# app = []
16+
17+
# def backtracking(last: int, left: int) -> None:
18+
# if left == 0:
19+
# ans.append(app[:])
20+
# return
21+
22+
# for i in range(last, N):
23+
# if candidates[i] > left:
24+
# continue
25+
26+
# app.append(candidates[i])
27+
# backtracking(i, left - candidates[i])
28+
# app.pop()
29+
30+
# backtracking(0, target)
31+
32+
# return ans
33+
34+
"""
35+
Time Complexity: O(N * target)
36+
Space Complexity: O(target)
37+
38+
- Use a dynamic programming approach to store the combinations that sum to each target.
39+
- dp[j] is a list of lists that sum to j.
40+
"""
41+
class Solution:
42+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
43+
dp = [list() for _ in range(target + 1)]
44+
dp[0] = [[]]
45+
46+
for c in candidates:
47+
for j in range(c, target + 1):
48+
for partial in dp[j - c]:
49+
dp[j].append(partial + [c])
50+
51+
return dp[-1]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Time Complexity: O(N)
3+
Space Complexity: O(1)
4+
5+
prev_2 : number of ways to decode the string ending with the previous two characters
6+
prev_1 : number of ways to decode the string ending with the previous character
7+
new_prev : number of ways to decode the string ending with the current character
8+
"""
9+
class Solution:
10+
def numDecodings(self, s: str) -> int:
11+
N = len(s)
12+
13+
if s[0] == "0":
14+
return 0
15+
16+
if N == 1:
17+
return 1
18+
19+
prev_2 = 1
20+
prev_1 = int(1 <= int(s[0] + s[1]) <= 26) + int(s[1] != "0")
21+
22+
for i in range(2, N):
23+
new_prev = 0
24+
if 1 <= int(s[i - 1] + s[i]) <= 26 and s[i - 1] != "0":
25+
new_prev += prev_2
26+
if s[i] != "0":
27+
new_prev += prev_1
28+
29+
prev_2, prev_1 = prev_1, new_prev
30+
31+
return prev_1
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
3+
"""
4+
Time Complexity: O(NLogN)
5+
Space Complexity: O(N) ( Reason: Recursive stack pointer for every element )
6+
7+
Divide and Conquer Approach
8+
9+
1. Divide the array into two halves
10+
2. Find the maximum subarray sum in the left half
11+
3. Find the maximum subarray sum in the right half
12+
4. Find the maximum subarray sum that crosses the midpoint
13+
5. Return the maximum of the three sums
14+
"""
15+
# class Solution:
16+
# def maxSubArray(self, nums: List[int]) -> int:
17+
18+
# def divide(start: int, end: int) -> int:
19+
# if start >= end:
20+
# return nums[start]
21+
22+
# mid = (start + end) // 2
23+
24+
# left = divide(start, mid)
25+
# right = divide(mid + 1, end)
26+
27+
# left_largest = -float('inf')
28+
# right_largest = -float('inf')
29+
30+
# left_acc = 0
31+
# right_acc = 0
32+
33+
# left_index = mid
34+
# right_index = mid + 1
35+
36+
# while left_index >= start:
37+
# left_acc += nums[left_index]
38+
# left_largest = max(left_largest, left_acc)
39+
# left_index -= 1
40+
41+
# while right_index <= end:
42+
# right_acc += nums[right_index]
43+
# right_largest = max(right_largest, right_acc)
44+
# right_index += 1
45+
46+
# return max(left_largest + right_largest, left, right)
47+
48+
# return divide(0, len(nums) - 1)
49+
50+
51+
52+
53+
"""
54+
Time Complexity: O(N)
55+
Space Complexity: O(1)
56+
57+
prev : maximum sum of the subarray ending with the previous element
58+
ans : maximum sum of the subarray
59+
curr : maximum sum of the subarray ending with the current element
60+
"""
61+
class Solution:
62+
def maxSubArray(self, nums: List[int]) -> int:
63+
N = len(nums)
64+
prev = nums[0]
65+
ans = nums[0]
66+
67+
for i in range(1, N):
68+
prev = max(nums[i], prev + nums[i])
69+
ans = max(ans, prev)
70+
71+
return ans
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
ans = 0
4+
while n:
5+
ans += n & 1
6+
n >>= 1
7+
return ans
8+
9+
# class Solution:
10+
# def hammingWeight(self, n: int) -> int:
11+
# return bin(n).count('1')
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Time Complexity: O(N)
3+
Space Complexity: O(N)
4+
5+
- Process string with lower() and isalnum() to remove non-alphanumeric characters
6+
- Check if the processed string is a palindrome by comparing it to its reverse
7+
"""
8+
# class Solution:
9+
# def isPalindrome(self, s: str) -> bool:
10+
# processed = "".join(ch.lower() for ch in s if ch.isalnum())
11+
12+
# return processed == processed[::-1]
13+
14+
"""
15+
Time Complexity: O(N)
16+
Space Complexity: O(1)
17+
18+
- Use two pointers to check if the string is a palindrome
19+
- Skip non-alphanumeric characters
20+
- Compare characters from both ends towards the center
21+
"""
22+
class Solution:
23+
def isPalindrome(self, s: str) -> bool:
24+
N = len(s)
25+
26+
left, right = 0, N - 1
27+
28+
while True:
29+
while not s[left].isalnum() and left < right:
30+
left += 1
31+
while not s[right].isalnum() and left < right:
32+
right -= 1
33+
34+
if left >= right:
35+
break
36+
37+
if s[left].lower() != s[right].lower():
38+
return False
39+
40+
left, right = left + 1, right - 1
41+
42+
return True

0 commit comments

Comments
ย (0)