From bc598915253f6b9fc435a7c2412acd18f49c5d90 Mon Sep 17 00:00:00 2001 From: Riccardo Doveri Date: Thu, 14 Apr 2022 23:01:50 +0200 Subject: [PATCH] Create 152-Maximum-Product-Subarray.java --- java/152-Maximum-Product-Subarray.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 java/152-Maximum-Product-Subarray.java 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; + } +}