-
Notifications
You must be signed in to change notification settings - Fork 51
Diana - Pine #41
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?
Diana - Pine #41
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,24 @@ | ||
|
|
||
| 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: ? | ||
| """ | ||
| Returns 0 if nums is None or an empty list. | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
| """ | ||
| if nums == None: | ||
| return 0 | ||
| if len(nums) == 0: | ||
| return 0 | ||
| pass | ||
| if len(nums) == 1: | ||
| return nums[0] | ||
| max_sum = nums[0] | ||
| current_sum = nums[0] | ||
| for i in range(1, len(nums)): | ||
| if current_sum + nums[i] > nums[i]: | ||
| current_sum += nums[i] | ||
| else: | ||
| current_sum = nums[i] | ||
|
Comment on lines
+18
to
+21
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 nice way to represent this calculation, which captures the underlying invariant that makes Kadane's algorithm work. Consider using current_sum = max(current_sum + nums[i], nums[i]) |
||
| if current_sum > max_sum: | ||
| max_sum = current_sum | ||
| return max_sum | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,20 @@ | ||
|
|
||
|
|
||
| # 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 | ||
| nc_nums = [1, 1] | ||
| if num < 1: | ||
| raise ValueError("Num must be greater than 0.") | ||
| for i in range(2, 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. Since you're always truncating the returned list using |
||
| nc_nums.append(nc_nums[nc_nums[i - 1] - 1] + nc_nums[i - nc_nums[i - 1]]) | ||
| string_list = [] | ||
| for n in nc_nums: | ||
| string_list.append(str(n)) | ||
|
Comment on lines
+15
to
+17
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 string_list = [str(n) for n in nc_nums]or string_list = map(str, nc_nums) |
||
| return " ".join(string_list[0: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. The need to slice the array here comes from needing to handle the base case for return " ".join(string_list) |
||
|
|
||
| # pass | ||
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.