- λ¬Έμ : https://leetcode.com/problems/maximum-product-subarray/
- νμ΄: https://algorithm.jonghoonpark.com/2024/07/09/leetcode-152
class Solution {
public int maxProduct(int[] nums) {
int max = Integer.MIN_VALUE;
int temp = 0;
int lastZeroIndex = 0;
for (int i = 0; i < nums.length; i++) {
int current = nums[i];
if (temp == 0) {
temp = current;
lastZeroIndex = i;
} else {
if (current == 0) {
temp = 0;
} else if (temp > 0 && current < 0) {
if (hasNextMinus(nums, i + 1)) {
temp = temp * current;
} else {
temp = temp * current;
for (int j = lastZeroIndex; j < i + 1; j++) {
temp = temp / nums[j];
if (temp > 0) {
break;
}
}
}
} else {
temp = temp * current;
}
}
max = Math.max(max, temp);
if (temp < 0 && !hasNextMinus(nums, i + 1)) {
temp = 0;
}
}
return max;
}
private boolean hasNextMinus(int[] nums, int i) {
for (; i < nums.length; i++) {
if (nums[i] < 0) {
return true;
} else if (nums[i] == 0) {
return false;
}
}
return false;
}
}μκ° λ³΅μ‘λλ O(n^2)μ΄λ€. κ³΅κ° λ³΅μ‘λλ O(1)μ΄λ€. hasNextMinus μ΄ μ κ² νΈμΆλλ€λ©΄ μκ° λ³΅μ‘λλ O(n)μ κ°κΉκ² λμνλ€.