Skip to content

Commit 71da372

Browse files
committedFeb 5, 2025·
maximum product subarray solved
1 parent 2123c0c commit 71da372

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
//시간복잡도: O(n)
3+
//공간복잡도: O(n)
4+
public int maxProduct(int[] nums) {
5+
int[] maxValue = new int[nums.length];
6+
int[] minValue = new int[nums.length];
7+
8+
maxValue[0] = nums[0];
9+
minValue[0] = nums[0];
10+
11+
for(int i = 1; i < nums.length; i++) {
12+
int value1 = maxValue[i - 1] * nums[i];
13+
int value2 = minValue[i - 1] * nums[i];
14+
15+
maxValue[i] = Math.max(Math.max(value1, value2), nums[i]);
16+
minValue[i] = Math.min(Math.min(value1, value2), nums[i]);
17+
}
18+
19+
int result = Integer.MIN_VALUE;
20+
for(int i = 0; i < nums.length; i++) {
21+
result = Math.max(result, maxValue[i]);
22+
}
23+
24+
return result;
25+
}
26+
}

0 commit comments

Comments
 (0)
Please sign in to comment.