-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
b59cf2e
8076db0
3a7c42b
791072c
e64125a
6a2d333
ae107c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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] | ||
return dp[len(s)] |
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:]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DP 문제를 다만, for 문에서 |
||
current_sum = max(num, current_sum + num) | ||
max_sum = max(max_sum, current_sum) | ||
return max_sum |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 처음에 문제의 제목에 충실하게 bit manipulation 관점으로만 접근했었는데요, printjin님의 풀이를 보고 수학적으로도 풀어볼 수 있었습니다! 감사합니다~! 😎 |
||
return cnt |
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] |
There was a problem hiding this comment.
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)으로 더 최적화 할 수 있을 것 같아요~!