1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| class Solution { public int maxProduct(int[] nums) { int n = nums.length; int[][] dp = new int[n][2]; dp[0][0] = nums[0]; dp[0][1] = nums[0]; int max = nums[0]; int min = nums[0]; int[] arr = new int[n];
for(int i = 1; i < n; i++){ if(nums[i] <= 0){ dp[i][0] = Math.min(nums[i], dp[i-1][1]*nums[i]); dp[i][1] = Math.max(nums[i], dp[i-1][0]*nums[i]); }else{ dp[i][0] = Math.min(nums[i], dp[i-1][0]*nums[i]); dp[i][1] = Math.max(nums[i], dp[i-1][1]*nums[i]); } }
int res = dp[0][1]; for(int i = 1; i < n; i++){ if(dp[i][1] > res){ res = dp[i][1]; } } return res; } }
|