Skip to content

Commit e2434fe

Browse files
committed
Time: 1 ms (97.49%), Space: 42.3 MB (89.61%) - LeetHub
1 parent f37eb8f commit e2434fe

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
class Solution {
22
public int maxProduct(int[] nums) {
3-
//base case
4-
if(nums.length == 0) return 0;
5-
//max_so_far
6-
int max_so_far = nums[0];
7-
//min_so_far
8-
int min_so_far = nums[0];
9-
//result
10-
int result = max_so_far;
113

12-
for(int i=1; i< nums.length; i++){
4+
int max = nums[0];
5+
int min = nums[0];
6+
7+
int result = max;
8+
9+
for(int i =1; i< nums.length; i++){
1310
int curr = nums[i];
14-
int temp_max = Math.max(curr, Math.max(max_so_far*curr, min_so_far*curr));
15-
min_so_far = Math.min(curr, Math.min(max_so_far*curr, min_so_far*curr));
16-
max_so_far = temp_max;
17-
result = Math.max(max_so_far,result);
11+
int temp_max = Math.max(curr,Math.max(max*curr,min*curr));
12+
min = Math.min(curr,Math.min(max*curr,min*curr));
13+
max = temp_max;
14+
result = Math.max(temp_max,result);
1815
}
1916
return result;
2017

2118
}
22-
2319
}

0 commit comments

Comments
 (0)