diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..eac4b4e 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,14 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) where n is the length of nums, we iterate over the array once, and perform constant time operations each loop +# Space Complexity: O(1) we only use constant sized variables def max_sub_array(nums) - return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + return 0 if nums == nil + best_total = nil + curr_total = 0 + nums.each do |num| + curr_total += num + best_total = curr_total if !best_total or curr_total > best_total + curr_total = 0 if curr_total < 0 + end + return best_total end diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..345f10d 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,18 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n) where n is the size of num +# Space Complexity: O(n) becuase we are creating a new array of size n + def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + raise ArgumentError if num <= 0 + sequence = Array.new(num) + num.times do |i| + if i < 2 + sequence[i] = 1 + else + previous = sequence[i - 1] + sequence[i] = sequence[previous - 1] + sequence[i - previous] + end + end + return sequence.join(" ") +end