forked from AdaGold/dynamic-programming
-
Notifications
You must be signed in to change notification settings - Fork 40
Julia Kingrey #32
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
Open
Kalakalot
wants to merge
5
commits into
Ada-C12:master
Choose a base branch
from
Kalakalot:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Julia Kingrey #32
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de60027
Passing base case assertions.
1840188
Passing all newman_conway tests which is very exciting because this i…
d798c7b
Adding time/space complexity and tweaks to notes.
05a3097
Changing not-so-great variable name to more descriptive
e3420db
Passing max subarray tests with time/space complexity added.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,26 @@ | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) because we have do our if/else operations on every element in the array | ||
| # Space Complexity: O(1) because we are re-using the same variable | ||
| def max_sub_array(nums) | ||
| return 0 if nums == nil | ||
|
|
||
| raise NotImplementedError, "Method not implemented yet!" | ||
| return nil if nums.empty? | ||
| return nums[0] if nums.length == 1 | ||
|
|
||
| max_so_far = max_ending_here = nums[0] | ||
|
|
||
| nums[1..-1].each do |number| | ||
|
|
||
| if max_ending_here + number < number | ||
| max_ending_here = number | ||
| else | ||
| max_ending_here += number | ||
| end | ||
|
|
||
| if max_so_far < max_ending_here | ||
| max_so_far = max_ending_here | ||
| end | ||
| end | ||
|
|
||
| return max_so_far | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,33 @@ | ||
|
|
||
|
|
||
| # Time complexity: ? | ||
| # Space Complexity: ? | ||
| # Time complexity: O(n) because we need to perform our "what is the p?" steps n times | ||
| # Space Complexity: also O(n) because we are adding an element to the sequence array for every n | ||
| def newman_conway(num) | ||
|
Comment on lines
+3
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. Really nice example of recursion and dynamic programming together. WEll done. |
||
| raise NotImplementedError, "newman_conway isn't implemented" | ||
| raise ArgumentError, "Argument must be an integer greater than zero." if num <= 0 | ||
|
|
||
| if num == 1 | ||
| return "1" | ||
| elsif num == 2 | ||
| return "1 1" | ||
| else | ||
| p_helper(num, [1, 1]) | ||
| end | ||
| end | ||
|
|
||
| def p_helper(num, sequence) | ||
|
|
||
| # BASE CASE: stop when count of numbers in sequence array is the same as `num` | ||
| return sequence.join(" ") if num == sequence.length | ||
|
|
||
| # REGRESSIVE CASE: add p for next integer in sequence | ||
| # forumula: p(num) = p(p(num - 1)) + p(num - p(num - 1)) | ||
| next_integer = sequence.length + 1 | ||
| # if num is 4 previous_p is at index 2 (value: 2) | ||
| previous_p = sequence[next_integer - 2] | ||
| # now we need to look up p of the previous_p value | ||
| p_previous_p = sequence[previous_p - 1] | ||
| # find p of number minus previous number's p, then add it to p_previous_p | ||
| p = p_previous_p + sequence[(next_integer - previous_p) - 1] | ||
| sequence << p | ||
| return p_helper(num, sequence) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍