Skip to content

Commit 1e4b7e1

Browse files
refactor 152
1 parent 5d2d00b commit 1e4b7e1

File tree

1 file changed

+13
-21
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+13
-21
lines changed
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 152. Maximum Product Subarray
5-
6-
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
7-
8-
For example, given the array [2,3,-2,4],
9-
the contiguous subarray [2,3] has the largest product = 6.
10-
*/
113
public class _152 {
12-
public static class Solution1 {
13-
public int maxProduct(int[] nums) {
14-
int pos = nums[0];
15-
int neg = nums[0];
16-
int max = nums[0];
17-
for (int i = 1; i < nums.length; i++) {
18-
int temp = pos;
19-
pos = Math.max(nums[i], nums[i] * ((nums[i] >= 0) ? pos : neg));
20-
neg = Math.min(nums[i], nums[i] * ((nums[i] >= 0) ? neg : temp));
21-
max = Math.max(max, pos);
22-
}
23-
return max;
4+
public static class Solution1 {
5+
public int maxProduct(int[] nums) {
6+
int pos = nums[0];
7+
int neg = nums[0];
8+
int max = nums[0];
9+
for (int i = 1; i < nums.length; i++) {
10+
int temp = pos;
11+
pos = Math.max(nums[i], nums[i] * ((nums[i] >= 0) ? pos : neg));
12+
neg = Math.min(nums[i], nums[i] * ((nums[i] >= 0) ? neg : temp));
13+
max = Math.max(max, pos);
14+
}
15+
return max;
16+
}
2417
}
25-
}
2618
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy