-
-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||||||||||||||||||||||||||||||||
| # 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 :) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||||||||
| def climbStairs(self, n: int) -> int: | ||||||||||||||||||||||||||||||||||||
| 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] | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+16
Contributor
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. you can simplify it a bit
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||
| #idea: dictionary | ||||
| from collections import Counter | ||||
|
|
||||
| class Solution: | ||||
| def isAnagram(self, s: str, t: str) -> bool: | ||||
| s_dic = Counter(sorted(s)) | ||||
| t_dic = Counter(sorted(t)) | ||||
| print(s_dic, t_dic) | ||||
|
Contributor
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. probably better to remove print statement to reduce IO time
Suggested change
|
||||
| return s_dic==t_dic | ||||
|
|
||||
|
|
||||
|
|
||||
| # Trial and Error | ||||
| ''' | ||||
| When you call sorted() on a dictionary, it only extracts and sorts the keys,and the values are completely ignored. | ||||
| class Solution: | ||||
| def isAnagram(self, s: str, t: str) -> bool: | ||||
| s_dic = Counter(s) | ||||
| t_dic = Counter(t) | ||||
| print(s_dic, t_dic) | ||||
| return sorted(s_dic)==sorted(t_dic) | ||||
| ''' | ||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
can take 1 or 2 stepsmeans that f(i) can be reached using:cannot rob adjacent housesmeans that f(i) can be calculated using the max between: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].
Uh oh!
There was an error while loading. Please reload this page.
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.
@ppxyn1
That's why you gotta focus on the
current affected by previouspart (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).
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!