-
Notifications
You must be signed in to change notification settings - Fork 51
Maple - Ruiz #37
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?
Maple - Ruiz #37
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,14 @@ | ||
| { | ||
| "python.testing.unittestArgs": [ | ||
| "-v", | ||
| "-s", | ||
| "./tests", | ||
| "-p", | ||
| "*test.py" | ||
| ], | ||
| "python.testing.pytestEnabled": true, | ||
| "python.testing.unittestEnabled": false, | ||
| "python.testing.pytestArgs": [ | ||
| "tests" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,20 @@ | |
| 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: On | ||
| Space Complexity: ON | ||
| """ | ||
| if nums == None: | ||
| return 0 | ||
| if len(nums) == 0: | ||
| return 0 | ||
| pass | ||
|
|
||
| max_sub_array = nums[0] | ||
| max_sub = nums[0] | ||
|
|
||
| for i in range(1,len(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. Another way we could skip over the first value is to slice the array, or use for num in islice(nums, 1, None):Then in the loop, replace |
||
|
|
||
| max_sub = max(max_sub + nums[i], nums[i]) | ||
| max_sub_array = max(max_sub_array, max_sub) | ||
|
|
||
| return max_sub_array | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,32 @@ | ||
|
|
||
|
|
||
| import array | ||
|
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. 👀 Unused import |
||
| # Time complexity: ? | ||
| # 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! 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 | ||
| # to store values | ||
| if num < 1: | ||
| raise ValueError ("Invalid Num") | ||
| if num == 1: | ||
| return "1" | ||
|
|
||
| f = [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. |
||
| output = "1" | ||
|
|
||
| count = 3 | ||
| # To store values of sequence in array | ||
|
|
||
| while count <= num: | ||
|
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. 👀 Prefer a |
||
| f.append(f[f[count - 1]]+ f[count - f [ count -1]]) | ||
| count += 1 | ||
| number = [str(item) for item in f] | ||
|
|
||
| return " ".join(number[1:]) | ||
|
Comment on lines
+25
to
+27
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 list comprehension to convert the numeric values to strings. Another approach would be to use the return " ".join(map(str, number[1:])) |
||
| # for i in range(3, num + 1): | ||
| # r = f[f[i-1]]+f[i-f[i-1]] | ||
| # f.append(r); | ||
|
|
||
| # return r | ||
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).
For the space complexity, even though we are working with an input list of size n, that's not part of our space usage (it was passed in to us). The only space we use is a few numbers to calculate the running maximum (and iterate), so like the dynamic programming approach to calculating the fibonacci sequence, we are able to maintain a sliding window of recent values to complete our calculation. This lets us implement this with a constant O(1) amount of storage.