-
Notifications
You must be signed in to change notification settings - Fork 51
C16 - Spruce - Vange Spracklin #45
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 |
|---|---|---|
|
|
@@ -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), iterates through relative to the size of the list given. | ||
| Space Complexity: ? O(1), we will only ever make 2 | ||
| """ | ||
| #guard clauses | ||
| if nums == None: | ||
| return 0 | ||
| if len(nums) == 0: | ||
| return 0 | ||
| pass | ||
|
|
||
| # initialize /memoize: | ||
| max_subarray_so_far = nums[0] | ||
| max_this_subarray = nums[0] | ||
|
|
||
| for i in range(1, len(nums)): # O(n) | ||
| max_this_subarray = max(max_this_subarray + nums[i], 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. You could also refer back to the pseudocode presented in the project description for a slightly different (though equivalent) approach. Again, to me it's not obvious why Kadane's algorithm is correct, so do check out that linked article. |
||
| max_subarray_so_far = max(max_this_subarray, max_subarray_so_far) | ||
|
|
||
| return max_subarray_so_far | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,30 @@ | ||
|
|
||
|
|
||
| # Time complexity: ? | ||
| # Space Complexity: ? | ||
| ''' | ||
| P(1) = 1 | ||
| P(2) = 1 | ||
| for all n > 2 | ||
| P(n) = P(P(n - 1)) + P(n - P(n - 1)) | ||
| ''' | ||
| 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) | ||
| """ | ||
| pass | ||
| # base case guard clauses | ||
| if num < 1: | ||
| raise ValueError('newman conway number cant be zero nums long') | ||
| elif num == 1: | ||
| return '1' | ||
|
|
||
| memo = [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. |
||
| count = 3 | ||
|
|
||
| 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 |
||
| # P(P(n - 1)) + P (n - P(n - 1)) | ||
| memo.append(memo[memo[count-1]] + memo[count - memo[count-1]]) | ||
| count += 1 | ||
|
|
||
| NCnums = [] | ||
| for num in memo: | ||
| NCnums.append(str(num)) | ||
|
Comment on lines
+26
to
+28
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. Consider using a comprehension or nc_nums = [str(num) for num in memo]or nc_nums = map(str, memo) |
||
| # print(NCnums[1:]) | ||
| return " ".join(NCnums[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.
✨ 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.