-
Notifications
You must be signed in to change notification settings - Fork 51
Roza - cedar #22
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?
Roza - cedar #22
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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^2) | ||||||||||||
| Space Complexity: O(1) | ||||||||||||
| """ | ||||||||||||
| if nums == None: | ||||||||||||
| return 0 | ||||||||||||
| if len(nums) == 0: | ||||||||||||
| return 0 | ||||||||||||
| pass | ||||||||||||
| max_overall = nums[0] | ||||||||||||
| max_so_far = nums[0] | ||||||||||||
| for index, num in enumerate(nums): | ||||||||||||
| so_far_sum = 0 | ||||||||||||
| for each in nums[:index + 1]: | ||||||||||||
| so_far_sum += each | ||||||||||||
| max_so_far = max(so_far_sum, num) | ||||||||||||
|
Comment on lines
+15
to
+18
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. With your current solution, you calculate what the sum would be for values from index 0 to the current index, and set 'max_so_far' to be either that calculated value or the current value you are iterating over. We can eliminate the need for that inner loop/calculation by instead just checking if If How would this change to your code affect your time and space complexity? 🤔
Suggested change
|
||||||||||||
| max_overall = max (max_overall, max_so_far) | ||||||||||||
|
|
||||||||||||
| return max_overall | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,24 @@ | ||
|
|
||
|
|
||
| # 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) | ||
|
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. ✨ |
||
| """ | ||
| pass | ||
| if num == 0: | ||
| raise ValueError | ||
| if num == 1: | ||
| return "1" | ||
| if num == 2: | ||
| return "1 1" | ||
| initial_array = [0, 1, 1] | ||
| for i in range(3, num + 1): | ||
| r = initial_array[initial_array[i-1]]+initial_array[i-initial_array[i-1]] | ||
| initial_array.append(r) | ||
| # remove the first 0 using list slices and change elements to str | ||
| stringified_array = [] | ||
| for elem in initial_array[1:]: | ||
| stringified_array.append(str(elem)) | ||
| return " ".join(stringified_array) | ||
|
|
||
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.
✨ This works and your time and space complexity does match your solution, but I think you're doing a little extra work here and we can make this solution even more efficient! See comments below ⬇️