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为分隔线将他们拆成多个序列来进行求积,这样 ...
随机推荐
- ASF (0) - ASF Java 项目总览
Apache .NET Ant Library This is a library of Ant tasks that help developing .NET software. It includ ...
- android ksoap2调用.net Webservice 方法总结
android ksoap2调用.net Webservice 方法直接放到一个类里: package com.util; import org.ksoap2.SoapEnvelope; impor ...
- J2EE的13个规范之JDBC
假设让你接触一样新的东西.你可能感觉无所适从,可是假设本来就是旧事物的话,你学习起来还难吗? 一.ODBC,我们的老朋友 ODBC(Open Database Connectivity)是微软公司与数 ...
- SQL:多表关联采取这一纪录迄今为止最大
笔者:iamlasong 1.需求 两个表,投递记录表和封发开拆记录表,如今想知道投递日期距最后一次封发日期天数分布情况. 对这个需求,须要先查询出投递明细,同一时候要知道相应的邮件最后一次封发情况. ...
- SoccerLeagueDB
create table if not exists League ( lid int primary key auto_increment, lyear int not null, s ...
- POJ1300(欧拉回路)
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2139 Accepted: 858 Descripti ...
- Codeforces 107B Basketball Team 简单概率
题目链接:点击打开链接 题意: 给定n m h 表示有m个部门,有个人如今在部门h 以下m个数字表示每一个部门的人数.(包含他自己) 在这些人中随机挑选n个人,问挑出的人中存在和这个人同部门的概率是多 ...
- 找出二叉树中和为n的路径
题目描述: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和 与输入整数相等的所有路径. 二叉树中的路径 从二叉树的根节点出发,至二叉树的叶子节点的 ...
- IOS成长之路-Nsstring搜索方法rangeOfString
NSString *str1 = @"can you \n speak English"; NSString *str = @"\n"; //在str1该字符串 ...
- 重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter
原文:重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresente ...