Question

152. Maximum Product Subarray

Solution

题目大意:求数列中连续子序列的最大连乘积

思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的,这个问题要注意正负,需要维护两个结果

Java实现:

public int maxProduct(int[] nums) {
if (nums.length == 1) return nums[0]; // 定义问题:状态及对状态的定义
// 设max[i]表示数列中第i项结尾的连续子序列的最大连乘积
// 求max[0]...max[n]中的最大值
// 状态转移方程
// max[0] = nums[0]
// max[i] = Max.max(max[i-1] * nums[i], nums[i])
int[] max = new int[nums.length];
int[] min = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
max[i] = min[i] = nums[i];
} int product = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
max[i] = Math.max(min[i - 1] * nums[i], max[i]);
min[i] = Math.min(max[i - 1] * nums[i], min[i]);
product = Math.max(max[i], product);
} else {
max[i] = Math.max(max[i - 1] * nums[i], max[i]);
min[i] = Math.min(min[i - 1] * nums[i], min[i]);
product = Math.max(max[i], product);
}
}
return product;
}

别人的实现

int maxProduct(int A[], int n) {
// store the result that is the max we have found so far
int r = A[0]; // imax/imin stores the max/min product of
// subarray that ends with the current number A[i]
for (int i = 1, imax = r, imin = r; i < n; i++) {
// multiplied by a negative makes big number smaller, small number bigger
// so we redefine the extremums by swapping them
if (A[i] < 0)
swap(imax, imin); // max/min product for the current number is either the current number itself
// or the max/min by the previous number times the current one
imax = max(A[i], imax * A[i]);
imin = min(A[i], imin * A[i]); // the newly computed max value is a candidate for our global result
r = max(r, imax);
}
return r;
}

关于动态规划

Ref

什么是动态规划?动态规划的意义是什么 - 知乎提问

152. Maximum Product Subarray - LeetCode的更多相关文章

  1. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  2. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  3. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  4. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  5. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  7. Maximum Product Subarray——LeetCode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...

  9. Java for LeetCode 152 Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. 【静态页面架构】CSS之链接和图像

    CSS架构 一.链接: 链接元素:通过使用a元素的href属性设置跳转到指定页面地址 <style> a{ color: blue; text-decoration: none; } a: ...

  2. the compatibility problem of ie

    ie8hack ie8下的兼容问题处理:背景透明,css3圆角,css3和jquery支持部分css3选择器(例如:nth-child),支持html5的语义化标签,媒体查询@media等. 在htm ...

  3. 探讨:微信小程序应该如何设计

    微信小程序公测后,开发者非常热情,都有很高的期待,都想抓住这一波红利.但是热情背后需要冷静,我们需要搞清楚两个问题: 微信想要我们做什么?微信小程序可以做什么? 微信想要我们做什么? 首先来弄清楚微信 ...

  4. 小程序 wx.getSystemInfoSync 获取 windowHeight 问题

    windowHeight 概念 可使用窗口高度,即:屏幕高度(screenHeight) - 导航(tabbar)高度 存在问题 安卓设备下获取 windowHeight 不能准确得到对应的高度,总是 ...

  5. 讲清楚之 javascript 参数传值

    讲清楚之 javascript 参数传值 参数传值是指函数调用时,给函数传递配置或运行参数的行为,包括通过call.apply 进行传值. 在实际开发中,我们总结javascript参数传值分为基本数 ...

  6. idea 提示错误: 找不到或无法加载主类

    首先检查自己的jdk 配置是否正确,检查好遍发现没有问题,但是项目就是运行不起来...... 重启idea,问题解决.

  7. 解决 Tomcat 控制台输出乱码(Tomcat Localhost Log / Tomcat Catalina Log 乱码)

    1. 按下图修改 先找到你的 Tomcat 安装目录,然后进入conf文件夹,找到 logging.properties,并打开它,然后把所有 UTF-8 格式的编码改成 GBK即可,具体操作如下图

  8. JavaScript实现带正则表达式的表单校验(校验成功后跳转)

    运行结果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...

  9. spring-基于注解的aop开发(快速入门)

    步骤: 1.导入坐标 <dependency> <groupId>junit</groupId> <artifactId>junit</artif ...

  10. Data详细解析