-
Notifications
You must be signed in to change notification settings - Fork 34
water - mackenzie #19
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 |
|---|---|---|
| @@ -1,8 +1,32 @@ | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n!)? You only go throught the each with index n times, but the inner loop completes decrementing number of loops... | ||
| # Space Complexity: O(1), no additional data structures created outside of 2 variables each times | ||
| def max_sub_array(nums) | ||
| return 0 if nums == nil | ||
|
|
||
| raise NotImplementedError, "Method not implemented yet!" | ||
| return nil if nums == [] | ||
| return nums[0] if nums.length == 1 | ||
|
|
||
| max_so_far = nums[0] | ||
| max_ending_here = 0 | ||
|
|
||
| nums.each_with_index do |num, index| | ||
| i = index + 1 | ||
|
|
||
| if num > max_so_far | ||
| max_so_far = num | ||
| end | ||
|
|
||
| max_ending_here = num | ||
|
|
||
| while i < nums.length do | ||
| max_ending_here = max_ending_here + nums[i] | ||
| if (max_so_far < max_ending_here) | ||
| max_so_far = max_ending_here | ||
| end | ||
| i+=1 | ||
| end | ||
|
Comment on lines
+21
to
+27
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. You can do this without the nested loop. Think about how you could use same saved values, like the largest subarray so far and the largest ending at the current index and how that could work. |
||
| end | ||
|
|
||
|
|
||
| return max_so_far | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,31 @@ | ||
|
|
||
|
|
||
| # Time complexity: ? | ||
| # Space Complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space Complexity: O(n), always creating 1 array num + 1 length long and 1 string proprtionate to n | ||
| # P(1) = 1 | ||
| # P(2) = 1 | ||
| # for all n > 2 | ||
| # P(n) = P(P(n - 1)) + P(n - P(n - 1)) | ||
| # 1 1 2 2 3 4 4 4 5 6 7 7…. | ||
| def newman_conway(num) | ||
|
Comment on lines
+3
to
10
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. 👍 |
||
| raise NotImplementedError, "newman_conway isn't implemented" | ||
| raise ArgumentError, "num must be > 0" if num < 1 | ||
|
|
||
| return "1" if num == 1 | ||
| return "1 1" if num == 2 | ||
|
|
||
| ncs_numbers = Array.new(num + 1) | ||
| ncs_numbers[1] = 1 | ||
| ncs_numbers[2] = 1 | ||
|
|
||
| n = 3 | ||
|
|
||
| ncs_string = "1 1" | ||
| while n <= num | ||
| ncs_numbers[n] = ncs_numbers[ncs_numbers[n-1]] + ncs_numbers[n - ncs_numbers[n-1]] | ||
| ncs_string << " " | ||
| ncs_string << ncs_numbers[n].to_s | ||
| n += 1 | ||
| end | ||
|
|
||
| return ncs_string | ||
| end | ||
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.