-
-
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
Conversation
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.
고생하셨습니다~! 🍀
if '1' <= s[i - 1] <= '9': | ||
dp[i] += dp[i - 1] | ||
if '10' <= s[i - 2:i] <= '26': | ||
dp[i] += dp[i - 2] |
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)으로 더 최적화 할 수 있을 것 같아요~!
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 comment
The reason will be displayed to describe this comment to others. Learn more.
DP 문제를 max_sum
과 current_sum
변수를 이용하여 O(n) space에서 O(1) space로 최적화 하신 걸로 보입니다!
다만, for 문에서 nums[1:]
로 한 번 슬라이싱하신다면 코드 전체의 공간 복잡도가 O(n)으로 계산될 것이기 때문에, O(1)으로 더 최적화 하시려면 인덱스로 for 문을 구성하시는 것도 좋은 방법일 것 같습니다 😆
cnt = 0 | ||
while n > 0: | ||
cnt += n % 2 | ||
n //= 2 |
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.
저는 처음에 문제의 제목에 충실하게 bit manipulation 관점으로만 접근했었는데요, printjin님의 풀이를 보고 수학적으로도 풀어볼 수 있었습니다! 감사합니다~! 😎
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!