-
Notifications
You must be signed in to change notification settings - Fork 51
Alf (Asli) - Spruce - C16 #44
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 |
|---|---|---|
|
|
@@ -2,11 +2,23 @@ | |
| 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 | ||
|
|
||
| max = - 10000000000000000000000000000000000000000000 | ||
|
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 the maximum to some value actually found in the list, say |
||
| curr_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. ✨ |
||
| curr_max += num | ||
| if curr_max > max: | ||
| max = curr_max | ||
| if curr_max < 0: | ||
| curr_max = 0 | ||
| return max | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,26 @@ | ||
|
|
||
|
|
||
| # Time complexity: ? | ||
| # Space Complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| 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! 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 (as is the converted string) giving space complexity of O(n) as well (ignoring a little bit of fiddliness related to the length of larger numbers being longer strings). |
||
| """ | ||
| pass | ||
| outputs = {} | ||
|
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. The structure we use to store the memo data can be a list, since we are carefully adding up from the bottom. A |
||
|
|
||
| res = [] | ||
| 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 | ||
|
|
||
| val = outputs[outputs[n-1]] + outputs[n - outputs[n - 1]] | ||
| outputs[n] = val | ||
| res.append(str(val)) | ||
|
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 to could be to calculate the set of values using a list as the backing memo structure, then convert the whole structure into strings all at once at the end. It's not really any more efficient, but by focusing on doing one thing in the main calculation (finding the needed numbers) and then doing the conversion separately, we separate the two phases a little which can help with understandability by somewhat separating the concerns. |
||
|
|
||
| return " ".join(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.
✨ 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.