152. Maximum Product Subarray - LeetCode
Question

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的更多相关文章
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- Maximum Product Subarray——LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- func-spring-boot-starter 匿名函数托管
func-spring-boot-starter 匿名函数托管 GitHub项目路径: https://github.com/yiurhub/func-spring-boot-starter Gite ...
- 攻防世界PHP2
PHP2 进入环境就一个英文其他啥都没有,英文也没啥提示信息 我们使用dirsearch扫描一下,一开始确实没扫到什么东西,到最后看了wp发现原来源码是在index.phps中,这里只提供一个思路,不 ...
- IT架构和架构类型
What is IT Architecture & Types of Architectures | ITARCH.INFO What is IT Architecture & Typ ...
- canvas —— globalCompositeOperation
globalCompositeOperation 属性设置或返回如何将一个源(新的)图像绘制到目标(已有)的图像上. 源图像 = 您打算放置到画布上的绘图. 目标图像 = 您已经放置在画布上的绘图. ...
- Javascript--function的name属性
1.非标准的name属性 function sayHi(){ console.log("Hi");} console.log(sayHi.name);
- 【Android开发】View 转 Bitmap
public static Bitmap loadBitmapFromView(View v) { int w = v.getWidth(); int h = v.getHeight(); Bitma ...
- 将子分支代码merge到主分支master分支
1.首先切换到子分支: git checkout develop2.使用git pull 把分支代码pull下来: git pull3.切换到主分支: git checkout master4.把分支 ...
- Docker 核心知识回顾
Docker 核心知识回顾 最近公司为了提高项目治理能力.提升开发效率,将之前的CICD项目扩展成devops进行项目管理.开发人员需要对自己的负责的项目进行流水线的部署,包括写Dockerfile ...
- Java-NIO之Channel(通道)
1:Channel是什么 通道表示与实体的开放连接,例如硬件设备.文件.网络套接字或能够执行一个或多个不同 I/O 操作(例如读取或写入)的程序组件. 1.1:Channel与Stream的对比 St ...
- JavaScript学习总结9
今天学习了表单提交,JQuery部分知识