DP Leetcode - Maximum Product Subarray
近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了。。思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积。最大乘积被更新有三种可能:当前A[i]>0,乘曾经面最大的数(>0),得到新的最大乘积;当前A[i]<0,乘曾经面最小的数(<0),得到新的最大乘积;A[i]它自己>0,(A[i-1]==0。最小乘积同理。。
class Solution {
public:
int Max(int a, int b, int c)
{
int max = -1 << 30;
if (a >= b)
max = a;
else
max = b;
if (c > max)
max = c;
return max;
}
int Min(int a, int b, int c)
{
int min = 1 << 30;
if (a <= b)
min = a;
else
min = b;
if (c < min)
min = c;
return min;
}
int maxProduct(int A[], int n) {
int maxPPrev=A[0], maxP;
int minPPrev = A[0], minP;
int max = A[0];
for (int i = 1; i < n; i++)
{
maxP = Max(maxPPrev * A[i], minPPrev * A[i], A[i]);
minP = Min(maxPPrev * A[i], minPPrev * A[i], A[i]);
maxPPrev = maxP;
minPPrev = minP;
if (maxPPrev > max)
max = maxPPrev;
if (minPPrev > max)
max = minPPrev;
}
return max;
}
};
DP Leetcode - Maximum Product Subarray的更多相关文章
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Maximum Product Subarray 连续数列最大积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152.[LeetCode] Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [leetcode]Maximum Product Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- LeetCode Maximum Product Subarray 最大子序列积
题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...
随机推荐
- Jsoup 抓取和数据页 认识HTTP头
推荐一本书:黑客攻防技术宝典.Web实战篇 : 顺便留下一个疑问:能否通过jsoup大量并发訪问web或者小型域名server,使其瘫痪?其有用jsoup熟悉的朋友能够用它解析url来干 ...
- WEB打印的几种方案
-------------------------------------------一 基于Web的打印方案比较分析-------------------------------- 基于web的套 ...
- 三层架构与MVC
三层简介 先说说Web三层架构这个古老话题.地球人都知道web三层架构是指: • >用户接口层(UI Layer) • >业务逻辑层(Bussiness Layer) • >持久化层 ...
- uva10791 uva10780(分解质因数)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Cocos2d-x 脚本语言Lua中的面向对象
Cocos2d-x 脚本语言Lua中的面向对象 面向对象不是针对某一门语言,而是一种思想.在面向过程的语言也能够使用面向对象的思想来进行编程. 在Lua中,并没有面向对象的概念存在,没有类的定义和子类 ...
- 动画(Animation) 它 (闪烁、左右摇摆、跷跷板等功效)
一侧到另一侧的影响: (这里显示的是并不那么顺利) 一.续播 (不知道取什么名字好,就是先播放动画A, 接着播放动画B) 有两种方式. 第一种.分别动画两个动画,A和B, 然后先播放动画A,设置A ...
- MFC漆摘要-截图,获得DIB/DDB图形Pixel
1. 当前Screen进行Copy屏幕,获得BITMAP 当前屏幕Copy.须要获取当前屏幕的HDC, 一种是直接从屏幕DC抓原始图. 一种是然后使用兼容MemDC进行抓图,然后能够附加图 ...
- 同一个存储过程中,不能多次select into 到同一张表的问题
表记录的插入方式有两种.其一,先create table 再 insert into from ....其二, 直接 select into. 第一种方式,由于要记录日志,因此IO消耗更多,durat ...
- Java面试题集(136-150)
摘要:目,尽管仅仅有15道题目.可是包括的信息量还是非常大的,非常多题目背后的解题思路和算法是非常值得玩味的. 136.给出以下的二叉树先序.中序.后序遍历的序列? 答:先序序列:ABDEGHCF.中 ...
- unicode下一个,读取数据库乱码问题
TCHAR cbContent[512]; dyn.GetFieldValue(0,cbContent,512); // 中文会显示乱码 AfxMessageBox(cbConte ...