Skip to content

[prograsshopper] Week 3 solution #1810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions combination-sum/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
# Time complexity: O(n ^ (T / m))
result = []

def dfs(remain_sum, index, path):
if remain_sum < 0:
return
if remain_sum == 0:
result.append(path)
return

for i in range(index, len(candidates)):
dfs(remain_sum - candidates[i], i, path + [candidates[i]])

dfs(target, 0, [])
return result
19 changes: 19 additions & 0 deletions decode-ways/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def numDecodings(self, s: str) -> int:
# sol 1: Time complexity O(n)
dp = {len(s): 1}

def dfs(start):
if start in dp:
return dp[start]
if s[start] == "0":
return 0
result = None
if start + 1 < len(s) and int(s[start:start+2]) < 27:
result = dfs(start+1) + dfs(start+2)
else:
result = dfs(start+1)
dp[start] = result
return result

return dfs(0)
10 changes: 10 additions & 0 deletions number-of-1-bits/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def hammingWeight(self, n: int) -> int:
# Time complexity: O(log n)
output = 1
while n > 1:
remain = n % 2
n = n // 2
if remain == 1:
output += 1
return output
18 changes: 18 additions & 0 deletions valid-palindrome/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
formatted_string = "".join(elem.lower() for elem in s if elem.isalnum())

# sol 1
# Time complexity: O(n)
return formatted_string == formatted_string[::-1]

# sol 2
# Time complexity: O(n)
Comment on lines +5 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공간 복잡도는 차이가 있으니 같이 표시해주시면 좋을 것 같네요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 넵 ㅎㅎ 다음 주차부터는 둘 다 표시하도록 하겠습니다!

left = 0
right = len(formatted_string) - 1
while left < right:
if formatted_string[left] != formatted_string[right]:
return False
left += 1
right -= 1
return True