Skip to content

[printjin-gmailcom] WEEK 03 solutions #1275

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 7 commits into from
Apr 16, 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
15 changes: 15 additions & 0 deletions combination-sum/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def combinationSum(self, candidates, target):
output, nums = [], []
def dfs(start, total):
if total > target:
return
if total == target:
return output.append(nums[:])
for i in range(start, len(candidates)):
num = candidates[i]
nums.append(num)
dfs(i, total + num)
nums.pop()
dfs(0, 0)
return output
13 changes: 13 additions & 0 deletions decode-ways/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def numDecodings(self, s):
if not s:
return 0
dp = [0] * (len(s) + 1)
dp[0] = 1
dp[1] = 1 if s[0] != '0' else 0
for i in range(2, len(s) + 1):
if '1' <= s[i - 1] <= '9':
dp[i] += dp[i - 1]
if '10' <= s[i - 2:i] <= '26':
dp[i] += dp[i - 2]
Copy link
Contributor

Choose a reason for hiding this comment

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

풀이가 정말 군더더기 없이 깔끔하고 가독성이 좋은 것 같습니다! 👍🏻

여기에 추가로 dp[i]를 구하기 위해 dp[i - 1]dp[i - 2]만 확인하기 때문에, dp 리스트 대신 변수 두 개를 이용해서 트래킹한다면 공간 복잡도를 O(n)에서 O(1)으로 더 최적화 할 수 있을 것 같아요~!

return dp[len(s)]
7 changes: 7 additions & 0 deletions maximum-subarray/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def maxSubArray(self, nums):
max_sum = current_sum = nums[0]
for num in nums[1:]:
Copy link
Contributor

Choose a reason for hiding this comment

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

DP 문제를 max_sumcurrent_sum 변수를 이용하여 O(n) space에서 O(1) space로 최적화 하신 걸로 보입니다!

다만, for 문에서 nums[1:]로 한 번 슬라이싱하신다면 코드 전체의 공간 복잡도가 O(n)으로 계산될 것이기 때문에, O(1)으로 더 최적화 하시려면 인덱스로 for 문을 구성하시는 것도 좋은 방법일 것 같습니다 😆

current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
7 changes: 7 additions & 0 deletions number-of-1-bits/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def hammingWeight(self, n):
cnt = 0
while n > 0:
cnt += n % 2
n //= 2
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 처음에 문제의 제목에 충실하게 bit manipulation 관점으로만 접근했었는데요, printjin님의 풀이를 보고 수학적으로도 풀어볼 수 있었습니다! 감사합니다~! 😎

return cnt
4 changes: 4 additions & 0 deletions valid-palindrome/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
def isPalindrome(self, s):
cleaned = [c.lower() for c in s if c.isalnum()]
return cleaned == cleaned[::-1]