题解 | #连续子数组的最大乘积#

连续子数组的最大乘积

https://www.nowcoder.com/practice/abbec6a3779940aab2cc564b22d36859

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型一维数组 
     * @return int整型
     */
    public int maxProduct (int[] nums) {
        // write code here
        int res = Integer.MIN_VALUE;

        int dpMax = 1;
        int dpMin = 1;
        for(int i = 0;i<nums.length;i++){
            int min = dpMin * nums[i];
            int max = dpMax * nums[i];
            dpMax = Math.max(max,Math.max(nums[i],min));
            dpMin = Math.min(max,Math.min(nums[i],min));
            res = Math.max(dpMax,res);
        }

        return res;
    }
}

全部评论
代码逻辑很清晰
点赞 回复 分享
发布于 2024-12-12 20:26 湖北

相关推荐

肖先生~:那年秋招闯进一位少年,人们都清楚:成功对他来说只是时间问题
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务