Skip to content

Commit 75cb47b

Browse files
authored
Merge pull request #2448 from jylee2033/main
[jylee2033] WEEK 03 solutions
2 parents 339aeaa + a4089d5 commit 75cb47b

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

combination-sum/jylee2033.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
output = []
4+
5+
def backtrack(remain, comb, start):
6+
if remain == 0:
7+
output.append(list(comb))
8+
return
9+
10+
elif remain < 0:
11+
return
12+
13+
for i in range(start, len(candidates)):
14+
comb.append(candidates[i])
15+
backtrack(remain - candidates[i], comb, i)
16+
comb.pop()
17+
18+
backtrack(target, [], 0)
19+
return output
20+
21+
# Time Complexity : O(N^T/M) N - number of candidates, T - target, M - minimum value in candidates
22+
# Space Complexity : O(T/M) - maximum depth of the recursion tree

number-of-1-bits/jylee2033.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
# # 1. Find highest power of 2 range that covers n
4+
# # 2. Greedily subtract from largest power of 2 to smallest
5+
6+
# # powers : 1 + 2 + 4 + 8 + 16 + ...
7+
# # total : 1 + 3 + 7 + 15 + 31 + ...
8+
# # index : 0 1 2 3 4
9+
10+
# i = 0
11+
# total = 1
12+
13+
# while n > total:
14+
# i += 1
15+
# total += 2 ** i
16+
17+
# count = 0
18+
19+
# for j in range(i, -1, -1):
20+
# if n >= 2 ** j:
21+
# n -= 2 ** j
22+
# count += 1
23+
24+
# return count
25+
26+
# Time Complexity : O(log n)
27+
# Space Complexity : O(1)
28+
29+
count = 0
30+
31+
while n > 0:
32+
if n % 2 == 1:
33+
count += 1
34+
35+
n //= 2
36+
37+
return count
38+
39+
# Time Complexity : O(log n)
40+
# Space Complexity : O(1)

valid-palindrome/jylee2033.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
# 1. Remove non-alphanumeric characters and convert to lowercase
4+
# 2. Compare with reversed string
5+
6+
# cleaned_s = ""
7+
# for ch in s:
8+
# if ch.isalnum():
9+
# cleaned_s += ch.lower()
10+
11+
# return cleaned_s == cleaned_s[::-1]
12+
13+
# Time Complexity : O(n)
14+
# Space Complexity : O(n)
15+
16+
left = 0
17+
right = len(s) - 1
18+
19+
while left < right:
20+
if not s[left].isalnum():
21+
left += 1
22+
elif not s[right].isalnum():
23+
right -= 1
24+
elif s[left].lower() != s[right].lower():
25+
return False
26+
else:
27+
left += 1
28+
right -= 1
29+
30+
return True
31+
32+
# Time Complexity : O(n)
33+
# Space Complexity : O(1)

0 commit comments

Comments
 (0)