-
Notifications
You must be signed in to change notification settings - Fork 51
Ivette F. - Spruce #31
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,7 @@ | ||
| { | ||
| "python.testing.pytestArgs": [ | ||
| "tests" | ||
| ], | ||
| "python.testing.unittestEnabled": false, | ||
| "python.testing.pytestEnabled": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,25 @@ | ||
|
|
||
| from json.encoder import INFINITY | ||
|
|
||
|
|
||
| def max_sub_array(nums): | ||
| """ Returns the max subarray of the given list of numbers. | ||
| Returns 0 if nums is None or an empty list. | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
|
Comment on lines
+8
to
+9
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. ✨ Notice how better time complexity this approach achieves over a "naïve" approach of checking for the maximum achievable sum starting from every position and every length. The correctness of this approach might not be apparent, so I definitely encourage reading a bit more about it. This has a fairly good explanation, as well as a description of why this is considered a dynamic programming approach (on the face it might not "feel" like one). Since like the fibonacci sequence, we are able to maintain a sliding window of recent values to complete our calculation, we can do it with a constant O(1) amount of storage. |
||
| """ | ||
| if nums == None: | ||
| return 0 | ||
| if len(nums) == 0: | ||
| return 0 | ||
| pass | ||
|
|
||
| maximum = - INFINITY | ||
|
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. Another approach would be to initialize maximum to some value actually found in the list, say |
||
| current_max = 0 | ||
|
|
||
| for num in nums: | ||
|
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. ✨ |
||
| current_max += num | ||
| if current_max > maximum: | ||
| maximum = current_max | ||
| if current_max < 0: | ||
| current_max = 0 | ||
| return maximum | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,31 @@ | |
| # Space Complexity: ? | ||
| def newman_conway(num): | ||
| """ Returns a list of the Newman Conway numbers for the given value. | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(n) | ||
|
Comment on lines
+7
to
+8
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. ✨ Great! We do only make one pass over the desired number of terms during the calculation. The the Storage-wise, we do need n items both in the dictionary and list, as well as the final returned string for O(3n) → O(n), again being a little hand-wavey with the overall size of the final string (and ignoring the sizes of the intermediate strings). |
||
|
|
||
| P(1) = 1 | ||
| P(2) = 1 | ||
| for all n > 2 | ||
| P(n) = P(P(n - 1)) + P(n - P(n - 1)) | ||
| """ | ||
| pass | ||
|
|
||
| # make a dict with key as n, and the value as P(n) (aka the output) | ||
|
|
||
| outputs = {} | ||
|
|
||
| res = [] | ||
|
Comment on lines
+18
to
+20
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. We don't really need two separate places to store the results. Since we are building up P(n) from smaller P() values, as long as we calculate the them in a strictly increasing order (as you do), we could store them in order in an array for later lookup. Using a dictionary can be more helpful if the repeated calculations happen in a less predictable order (often when we are using a recursion+memoization approach). The other thing you're using the two representations for is to store the numerical result in one, and the string representation in the other. We could covert the numerical values to strings as part of joining them together by using Otherwise, great approach! |
||
| if num <= 0: | ||
| raise ValueError | ||
|
|
||
| for n in range(1, num + 1): | ||
| if n == 1 or n == 2: | ||
| outputs[n] = 1 | ||
| res.append("1") | ||
| continue | ||
| # P(n -1) -> outputs[ n - 1] | ||
| value = outputs[outputs[n - 1]] + outputs[n - outputs[n - 1]] | ||
| outputs[n] = value | ||
| res.append(str(value)) | ||
|
|
||
| return " ".join(res) | ||
|
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. As mentioned above, we could store the numerical values in return " ".join(map(str, res))or return " ".join(str(num) for num in res) |
||
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.
👀 The
INFINITYsymbol is really just an alias forfloat('inf'), which you can access without needing to import anything from thejsonmodule.