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 ...
随机推荐
- Leetcode刷题之矩阵中的指针用法
矩阵中的指针用法 1 快慢指针 Leetcode27移除元素 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度.不要使用额外的数组 ...
- Noob渗透笔记
靶机下载地址:https://www.vulnhub.com/entry/noob-1,746/ kali ip 信息收集 依旧我们先使用nmap扫描确定一下靶机ip nmap -sP 192.168 ...
- 设计模式之简单工厂SimpleFactory的实现(配置文件)
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSetti ...
- 关于recyclerview其item数据重复问题
查找方法(query)的list只定义对象,不实例化,等到要添加的时候,再new一个新的对象出来. 千万不要如下图这样,否则item显示出来的永远是最新数据. (这个bug找了两天,还是基本功不扎实, ...
- Spark周总结(一)
本周学习内容: 1.搭建虚拟机Spark环境 2.idea编写Scala脚本并在yarn上运行 总结: 这周是回家第一周,虽然没啥事,但是还是想放松放松,也是万事开头难,跟着教程做,但总有几步跟教程上 ...
- 子线程中如何修改ui界面
1.Android进程 一个应用程序被启动时,系统默认创建执行一个叫做"main"的线程.这个线程也是你的应用与界面工具包(android.widget和android.view包 ...
- 微信支付之微信H5支付(坑,ajax不支持重定向跳转)
这里讲的是 微信h5支付, 是微信以外的手机浏览器调用微信h5支付 h5支付: H5支付是指商户在微信客户端外的移动端网页展示商品或服务,用户在前述页面确认使用微信支付时,商户发起本服务呼起 ...
- git clone 遇到的问题
经常遇到这个问题, 所以今天决定把它记录下来 当使用git clone时,会报Please make sure you have the correct access rights and the r ...
- 【合集】Python基础知识【第二版】
更新部分 为了避免冗长的代码影响大家观感,将部分案例拆开 增加部分知识点,为了减少大家阅读的负担,尽可能使用短句子,但知识点不可能全覆盖,笔者不是写书,就算是写书也不可能全面,请同学们自行去补充 增加 ...
- 面试 | Java 算法的 ACM 模式
(Java 算法的 ACM 模式) 前言 经常在 LeetCode 上用核心代码模式刷题的小伙伴突然用 ACM 模式可能会适应不过来,把时间花在输入输出上很浪费时间,因此本篇笔记对 Java 算法的 ...