Skip to content

Commit 89f415f

Browse files
Create 152-Maximum-Product-Subarray.rb
1 parent 23bc8a8 commit 89f415f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

ruby/152-Maximum-Product-Subarray.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# @param {Integer[]} nums
2+
# @return {Integer}
3+
def max_product(nums)
4+
5+
@largest = -Float::INFINITY
6+
@min = 1
7+
@max = 1
8+
9+
for num in nums do
10+
@temp = @max * num
11+
@max = [num * @max, num * @min, num].max
12+
@min = [num * @min, @temp, num].min
13+
@largest = [@max, @largest, num].max
14+
end
15+
16+
return @largest
17+
end

0 commit comments

Comments
 (0)