-
Notifications
You must be signed in to change notification settings - Fork 51
Lilly Pine C16 #33
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?
Lilly Pine C16 #33
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": [ | ||
| "." | ||
| ], | ||
| "python.testing.unittestEnabled": false, | ||
| "python.testing.pytestEnabled": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,21 @@ | |
| 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) | ||
| """ | ||
| if nums == None: | ||
| return 0 | ||
| if len(nums) == 0: | ||
| return 0 | ||
| pass | ||
| n = len(nums) | ||
| max_sum = -10000000000 | ||
|
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. 👀 What if the list were made of negative numbers all below -10000000000? We could use an impossibly small value (like |
||
| curr_sum = 0 | ||
|
|
||
| for i in range(n): | ||
|
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. ✨ |
||
| curr_sum += nums[i] | ||
| if curr_sum > max_sum: | ||
| max_sum = curr_sum | ||
| if curr_sum < 0: | ||
| curr_sum = 0 | ||
| return max_sum | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,28 @@ | |
|
|
||
| # Time complexity: ? | ||
| # Space Complexity: ? | ||
| def newman_conway(num): | ||
| def newman_conway(nums): | ||
| """ Returns a list of the Newman Conway numbers for the given value. | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(n^2) | ||
|
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. 👀 By carefully building up the calculations and storing them for later use, we only need to perform O(n) calculations. The storage to keep those calculations is related to n, giving space complexity of O(n) as well (ignoring a little bit of fiddliness related to the length of larger numbers being longer strings). However, when building up the string representation, performing repeated string concatenation ends up recopying the intermediate strings with each new append. Now there's only ever the previous string and the new string in memory simultaneously, so the space complexity remains O(n), however, the time complexity actually becomes O(n^2). So this implementation has time complexity O(n^2) and space complexity O(n). |
||
| """ | ||
| pass | ||
| if nums == 0: | ||
| raise ValueError | ||
|
Comment on lines
+10
to
+11
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 should raise this error for any value below the valid starting point of the sequence: if nums <= 0:
raise ValueError |
||
|
|
||
| if nums == 1: | ||
| return "1" | ||
|
|
||
| if nums == 2: | ||
| return "1 1" | ||
|
|
||
| seq = "" | ||
| start_list = [0, 1, 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. ✨ Nice use of a buffer slot to account for the 1-based calculation. |
||
| for i in range(3,nums+1): | ||
| start_list.append( start_list[start_list[i - 1]] + start_list[i - start_list[i - 1]]) | ||
|
|
||
| for num in start_list[1:]: | ||
| seq += str(num) + " " | ||
|
Comment on lines
+24
to
+25
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. 👀 This repeated concatenation is what causes the O(n^2) time complexity. Rather than repeated string concatenations, it's preferred to build up a list of values, and then return " ".join(map(str, start_list[1:])) |
||
| return seq.strip() | ||
|
|
||
|
|
||
|
|
||
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.
✨ 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.