-
Notifications
You must be signed in to change notification settings - Fork 51
cedar mac #43
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?
cedar mac #43
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,14 @@ | ||
|
|
||
| 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: ? | ||
| """ | ||
|
|
||
| if nums == None: | ||
| return 0 | ||
| if len(nums) == 0: | ||
| return 0 | ||
| pass | ||
|
|
||
| max_array = nums[0] | ||
| current_max = nums[0] | ||
|
|
||
| for i in range(1, len(nums)): | ||
| current_max = max(nums[i], current_max + nums[i]) | ||
|
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 is a really nice way to represent this calculation, which captures the underlying invariant that makes Kadane's algorithm work. This article has a fairly good explanation of why this is considered a dynamic programming approach (on the face it might not "feel" like one). |
||
| max_array = max(current_max, max_array) | ||
| return max_array | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,24 @@ | ||
|
|
||
|
|
||
| # Time complexity: ? | ||
| # Space Complexity: ? | ||
| def newman_conway(num): | ||
| """ Returns a list of the Newman Conway numbers for the given value. | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
|
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. 👀 Time and space complexity? |
||
| """ | ||
| pass | ||
| if num == 0: | ||
| raise ValueError | ||
|
Comment on lines
+4
to
+5
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 |
||
| if num == 1: | ||
| return "1" | ||
|
|
||
| memo = [None] * (num + 1) | ||
|
|
||
| memo[0] = 0 | ||
|
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. |
||
| memo[1] = 1 | ||
| memo[2] = 1 | ||
|
|
||
| i = 3 | ||
| while i <= 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 |
||
| memo[i] = memo[memo[i - 1]] + memo[i - memo[i - 1]] | ||
| i += 1 | ||
|
|
||
| result = "" + str(memo[1]) | ||
| for i in range(2, len(memo)): | ||
| result += " " + str(memo[i]) | ||
|
Comment on lines
+21
to
+22
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. 👀 Repeated concatenation causes the O(n^2) time complexity (due to each concatenation needing to copy every previous concatenation). Rather than repeated string concatenations, it's preferred to build up a list of values, and then return " ".join(map(str, memo[1:]))which converts each of the numbers in |
||
|
|
||
| return result | ||
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.
👀 Time and space complexity?