diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..bbb9566 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -2,7 +2,25 @@ # Time Complexity: ? # Space Complexity: ? def max_sub_array(nums) - return 0 if nums == nil + return nil if (nums == nil || nums.length === 0) + + max = nums[0] + temp = 0 + + nums.each do |num| + temp += num - raise NotImplementedError, "Method not implemented yet!" + if temp > max + max = temp + end + + if temp < 0 + temp = 0 + end + end + + return max end + + +# p max_sub_array([-2,1,-3,4,-1,2,1,-5,4]) \ No newline at end of file diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..db79e8a 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,23 @@ +# Time complexity: O(n) +# Space Complexity: O(n) +def newman_conway(num) + return nc_helper(num) +end +def nc_helper(num, count = 2, memo = [0, 1, 1], result = "1 1") + raise ArgumentError if num <= 0 + return "1" if num == 1 + return "1 1" if num == 2 + + if count == num + return result + end + + value = memo[memo[count]] + memo[count + 1 - memo[count]] + memo << value + result << " #{value}" + + return nc_helper(num, count + 1, memo, result) +end -# Time complexity: ? -# Space Complexity: ? -def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file +# p newman_conway(12) \ No newline at end of file