diff --git a/java/152-Maximum-Product-Subarray.java b/java/152-Maximum-Product-Subarray.java new file mode 100644 index 000000000..cb7857f14 --- /dev/null +++ b/java/152-Maximum-Product-Subarray.java @@ -0,0 +1,15 @@ +class Solution { + public int maxProduct(int[] nums) { + int res = nums[0]; + int max = 1; + int min = 1; + + for (int n: nums) { + int tmp = max * n; + max = Math.max(n, Math.max(tmp, min * n)); + min = Math.min(n, Math.min(tmp, min * n)); + res = Math.max(res, max); + } + return res; + } +}