We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6060770 commit 97242fbCopy full SHA for 97242fb
โmaximum-product-subarray/eunhwa99.java
@@ -0,0 +1,26 @@
1
+class Solution {
2
+
3
+ // TP: O(N)
4
+ // SP: O(1)
5
+ // ์์ ์์๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ๊ณฑ์ Min ๊ฐ๋ ์๊ฐํด์ผ ํ๋ ๋ฌธ์ !
6
+ public int maxProduct(int[] nums) {
7
8
+ int minProd = nums[0];
9
+ int maxProd = nums[0];
10
+ int result = nums[0];
11
+ for (int i = 1; i < nums.length; i++) {
12
+ if (nums[i] < 0) {
13
+ int temp = minProd;
14
+ minProd = maxProd;
15
+ maxProd = temp;
16
+ }
17
18
+ maxProd = Math.max(nums[i], maxProd * nums[i]);
19
+ minProd = Math.min(nums[i], minProd * nums[i]);
20
+ result = Math.max(result, maxProd);
21
22
23
+ return result;
24
25
+}
26
0 commit comments