easy to understand dp solution by java

  • -1

    for ith in nums, minCurr just means the min product to i-1
    maxCurr just means the max product to i-1
    the maxCurr,minCurr can only get from maxCurrcurr,minCurrcurr,curr
    public class Solution {
    public int maxProduct(int[] nums) {
    int res = nums[0];
    int minCurr = nums[0];
    int maxCurr = nums[0];
    for(int i=1;i<nums.length;i++){
    int curr = nums[i];
    int nMinCurr = Math.min(maxCurrcurr,Math.min(minCurrcurr,curr));
    int nMaxCurr = Math.max(minCurrcurr,Math.max(maxCurrcurr,curr));
    minCurr = nMinCurr;
    maxCurr = nMaxCurr;
    res = maxCurr;
    return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.