-
-
Notifications
You must be signed in to change notification settings - Fork 304
[ppxyn1] WEEK 02 solutions #2030
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
| def isAnagram(self, s: str, t: str) -> bool: | ||
| s_dic = Counter(sorted(s)) | ||
| t_dic = Counter(sorted(t)) | ||
| print(s_dic, t_dic) |
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.
probably better to remove print statement to reduce IO time
| print(s_dic, t_dic) |
| if n <= 2: | ||
| return n | ||
| dp = [0] * (n+1) | ||
| dp[2], dp[3] = 2, 3 | ||
|
|
||
| #for i in range(4, n): error when n=4 | ||
| for i in range(4, n+1): | ||
| dp[i] = dp[i-1] + dp[i-2] | ||
|
|
||
| return dp[n] |
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.
you can simplify it a bit
| if n <= 2: | |
| return n | |
| dp = [0] * (n+1) | |
| dp[2], dp[3] = 2, 3 | |
| #for i in range(4, n): error when n=4 | |
| for i in range(4, n+1): | |
| dp[i] = dp[i-1] + dp[i-2] | |
| return dp[n] | |
| dp = [0] * (n+1) | |
| dp[0], dp[1] = 1, 1 | |
| for i in range(2, n + 1): | |
| dp[i] = dp[i-1] + dp[i-2] | |
| return dp[n] |
| # idea: DP | ||
| # I'm not always sure how to approach DP problems. I just try working through a few examples step by step and then check that it would be DP. | ||
| # If you have any suggestions for how I can come up with DP, I would appreciate your comments :) |
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.
my rule of thumb is checking if current value(aka value at index) can be determined using the previous.
for instance, both #230 and #264 are both similar in that desired value at index can be determined using previous values.
- Climbing Stairs #230 :
- f(i) = # of possible combinations to reach i
can take 1 or 2 stepsmeans that f(i) can be reached using:- f(i-1)—taking 1 step—and
- f(i-2)—taking 2 steps—
- thus, f(i) = f(i-1) + f(i-2)
- House Robber #264 :
- f(i) = max possible amount that can be robbed to reach i
cannot rob adjacent housesmeans that f(i) can be calculated using the max between:- f(i-1)—choosing not to rob house[i]—and
- f(i-2)—choosing to rob house[i]
- f(i) = max(f(i-1), f(i-2)) + house[i]
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.
Thank you for your detailed explanation! it was really helpful:)
I haven’t solved that many problems yet, so this might be why I have one more question: isn’t it easy to miss that a value can be determined using previous values when the step size is larger? For instance, dp[6] could be obtained from dp[1] and dp[5].
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.
That's why you gotta focus on the current affected by previous part (kinda like Induction proof from discrete math courses)
Begin with smaller n's and see if there is a pattern.
Take #230 with max step size up to 4 as an example(6 is too big for a written explanation of patterns).
- n=0: There's only one way (0)
- n=1: There's only one way (0->1)
- n=2: 2 because (0 -> 2 or 0 -> 1 -> 2)
- f(0): 0 -> 2
- f(1): 0 -> 1 -> 2
- Already kinda seeing the pattern since we're seeing f(2) = f(0) + f(1)
- n=3: 4
- f(0): 0 -> 3
- f(1): 0 -> 1 -> 3
- f(2):
- 0 -> 2 -> 3
- 0 -> 1 -> 2 -> 3
- n=4: 8
- f(0): 0 -> 4
- f(1): 0 -> 1 -> 4
- f(2):
- 0 -> 2 -> 4
- 0 -> 1 -> 2 -> 4
- f(3):
- 0 -> 3 -> 4
- 0 -> 1 -> 3 -> 4
- 0 -> 2 -> 3 -> 4
- 0 -> 1 -> 2 -> 3 -> 4
So the pattern is:
f(n) = f(n-1) + f(n-2) + .... f(n-k), where k is the step size
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.
Appreciate it!
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.
현재 주차가 종료되어 자동으로 승인되었습니다. PR을 병합해주세요!
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!