-
Notifications
You must be signed in to change notification settings - Fork 51
Jiajia(Grace) Wang #39
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,4 +1,4 @@ | ||
|
|
||
| import math | ||
| 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. | ||
|
|
@@ -9,4 +9,16 @@ def max_sub_array(nums): | |
| return 0 | ||
| if len(nums) == 0: | ||
| return 0 | ||
| pass | ||
| largest_sum = [0] * len(nums) | ||
| largest_sum[0] = 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. ✨ Nice approach that really shows why Kadane's algorithm is really a form of dynamic programming. 👀 What's the time and space complexity of this approach? Could we make an adjustment to who we're carrying along the previous results to improve the space complexity a bit more? |
||
| if largest_sum[i - 1] <= 0: | ||
| largest_sum[i] = nums[i] | ||
|
|
||
| else: | ||
| largest_sum[i] = nums[i] + largest_sum[i-1] | ||
|
|
||
| return max(largest_sum) | ||
|
|
||
|
|
||
| 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 | ||
| nc_list = [0] * (num + 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. |
||
| if num == 0: | ||
| raise ValueError("n must be > 0") | ||
|
Comment on lines
+11
to
+12
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 num <= 0:
raise ValueError("n must be > 0") |
||
| if num == 1: | ||
| return "1" | ||
| if num == 2: | ||
| return "1 1" | ||
| if num > 2: | ||
| nc_list[1] = 1 | ||
| nc_list[2] = 1 | ||
| for i in range(3, num + 1): | ||
|
|
||
| nc_list[i] = nc_list[nc_list[i-1]] + nc_list[i - nc_list[i-1]] | ||
|
|
||
| return " ".join(str(v) for v in nc_list[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 generator to convert the numeric results to strings. This is a generator rather than a list comprehension because it lacks the Another approach would be to make uses of the return " ".join(map(str, nc_list[1:])) |
||
| print(newman_conway(1)) | ||
|
|
||
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.
👀 Not used