Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Explanation:
- Need to track both max and min as two large negative numbers would make a large positive number. So track curr_min, curr_max, prev_min, prev_max.
- */
- class Solution {
- public int maxProduct(int[] nums) {
- int curr_max, curr_min, prev_max, prev_min, res;
- curr_max = curr_min = prev_max = prev_min = res = nums[0];
- for (int i = 1; i < nums.length; i++) {
- curr_max = Math.max(Math.max(nums[i], prev_max*nums[i]), prev_min*nums[i]);
- curr_min = Math.min(Math.min(nums[i], prev_max*nums[i]), prev_min*nums[i]);
- res = Math.max(curr_max, res);
- prev_max = curr_max;
- prev_min = curr_min;
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement