leetcode-Maximum Product Subarray-ZZ
http://blog.csdn.net/v_july_v/article/details/8701148
假设数组为a[],直接利用动归来求解,考虑到可能存在负数的情况,我们用Max来表示以a结尾的最大连续子串的乘积值,用Min表示以a结尾的最小的子串的乘积值,那么状态转移方程为:
Max=max{a[i], Max[i-1]*a[i], Min[i-1]*a[i]};
Min=min{a[i], Max[i-1]*a[i], Min[i-1]*a[i]};
初始状态为Max[0]=Min[0]=a[0]。
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxProduct(int A[], int n) {
int *maxArray = new int[n];
int *minArray = new int[n];
maxArray[] = minArray[] = A[];
int result=maxArray[];
for (int i = ; i < n; i++)
{
maxArray[i] = max(max(maxArray[i-]*A[i],minArray[i-]*A[i]),A[i]);
minArray[i] = min(min(maxArray[i-]*A[i],minArray[i-]*A[i]),A[i]);
result = max(result,maxArray[i]);
}
return result;
}
};
int main()
{
Solution s;
int n = ;
int a[] = {,,-,};
cout << s.maxProduct(a,)<<endl;
return ;
}
==============================================================================================
LinkedIn - Maximum Sum/Product Subarray
Maximum Sum Subarray是leetcode原题,跟Gas Station的想法几乎一模一样。解答中用到的结论需要用数学简单地证明一下。
1
2
3
4
5
6
7
8
9
10
11
12
|
public int maxSubArray( int [] A) { int sum = 0 ; int max = Integer.MIN_VALUE; for ( int i = 0 ; i < A.length; i++) { sum += A[i]; if (sum > max) max = sum; if (sum < 0 ) sum = 0 ; } return max; } |
Maximum Product Subarray其实只需要不断地记录两个值,max和min。max是到当前为止最大的正product,min是到当前为止最小的负product,或者1。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public int maxProduct( int [] A) { int x = 1 ; int max = 1 ; int min = 1 ; for ( int i = 0 ; i < A.length; i++) { if (A[i] == 0 ) { max = 1 ; min = 1 ; } else if (A[i] > 0 ) { max = max * A[i]; min = Math.min(min * A[i], 1 ); } else { int temp = max; max = Math.max(min * A[i], 1 ); min = temp * A[i]; } if (max > x) x = max; } return x; } |
http://shepherdyuan.wordpress.com/2014/07/23/linkedin-maximum-sumproduct-subarray/
leetcode-Maximum Product Subarray-ZZ的更多相关文章
- 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 ...
- 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 连续数列最大积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [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为分隔线将他们拆成多个序列来进行求积,这样 ...
- DP Leetcode - Maximum Product Subarray
近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...
随机推荐
- hive DML
1.load files into tables 把文件中的数据加载到表中(表必须先建好) 语法是: load data [local] inpath 'filepath' [overwrite] i ...
- line-height详解
line-height详解 要说line-height就必须要知道这几个概念了: 顶线.中线.基线.底线. 这也就是在vertical-align中可能用到的top,middle,baseline和b ...
- Mybatis多参数
转载自:一杯甜酒 http://blog.csdn.net/u012562943/article/details/52316071 据我目前接触到的传多个参数的方案有三种.第一种方案 DAO层的函数方 ...
- wtl学习总结
在windows平台,相比MFC,我更喜欢WTL,因其简洁漂亮.所以陆续花了一年的时间学习之,这里总结一下(在学习Wtl/Atl之前,最好是对WinApi编程有一定的了解). 安装 Wtl主页 htt ...
- Magento 2中文手册教程 - 如何获得 Magento 2
Magento 2 安装 我们搜集了一些信息来帮助您开始使用Magento 2和你的Magento 2安装. 我们有一些资源帮助您开始使用Magento 2. 如何获得 Magento 2 参考下表开 ...
- net 记录controller Action耗时
可能有些时候需要记录Action的执行时间来优化系统功能,这时可以用过滤器来实现 第1个例子 using System; using System.Diagnostics; using System. ...
- 创建一个自定义的Application类
由于每个应用程序必须创建一个Application对象,vs为开发人员提供了模板来减轻开发人员的重复工作.当使用vs创建一个WPF应用程序是,vs会自动创建一个app.xaml文件, <Appl ...
- 07.重写ToSting()方法
namespace _08.重写ToString方法 { class Program { static void Main(string[] args) { Person p = new Person ...
- Java API 之 Annotation功能
JDK1.5开始增加了Annotation功能,该功能可用于: 1.类: 2.构造方法: 3.成员变量: 4.方法 5.参数 等的声明: 该功能并不影响程序的运行,但是会对编译器警告等辅助工具产生影响 ...
- thinkphp怎么把数据库中的列的值存到下拉框中
1. 先去数据库中查值,查询整个数据表,结果为二维数组. $project = M("project"); $cell = $project->where(array('st ...