g0101_0200.s0152_maximum_product_subarray.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g0101_0200.s0152_maximum_product_subarray;
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
// #Big_O_Time_O(N)_Space_O(1) #2024_11_15_Time_1_ms_(92.74%)_Space_45_MB_(23.41%)
public class Solution {
public int maxProduct(int[] nums) {
int overAllMaxProd = Integer.MIN_VALUE;
int n = nums.length;
int start = 1;
int end = 1;
for (int i = 0; i < n; i++) {
if (start == 0) {
start = 1;
}
if (end == 0) {
end = 1;
}
start = start * nums[i];
end = end * nums[n - i - 1];
overAllMaxProd = Math.max(overAllMaxProd, Math.max(start, end));
}
return overAllMaxProd;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy