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]> ...
随机推荐
- element-ui tree树形组件自定义实现可展开选择表格
最近做项目遇到一个需求,表格里可以展开,可以选择,大概效果如下图: 一开始是在table组件里找方法,使用了表格的合并方法,效果是实现了,但是表格的多选每次触发时,都会执行好几次,而且没法实现一部分的 ...
- @Transcational特性
捕获RuntimeException 捕获Error 并不捕获Checked Exception 在方法中使用@Transcational注解时候,通过throw new Exception(),在发 ...
- 关于cg语言中求法向量 N=mul(worldMatrix_IT,normal); 的随笔
解释一下标题,N是变换到世界坐标后的法向量,worldMatrix_IT是变换矩阵worldMatrix的逆的转置矩阵,normal就是模型坐标的法向量. 对于点p,我们根据变换矩阵M(即worldM ...
- Git删除和恢复文件
删除文件: 如果你在本地删除了一个文件但是没有提交到版本库,这时你用 $ git status命令会看到提示: 如果需要从版本库中删除该文件,则需要用 $ git rm 和 $ git commit ...
- Ubuntu 18.04 安装 odoo12 源码版
更新和升级 在我们进入安装过程之前,你应该更新和升级Ubuntu.打开终端窗口,发出以下命令: sudo apt-get updatesudo apt-get upgrade 注意:如果内核升级,则必 ...
- 不存在类型或命名空间名称“HttpContext”
错误:命名空间“System.Web”中不存在类型或命名空间名称“HttpContext”(是缺少程序集引用吗?) 解决办法:创建类库项目时,默认是没有添加System.Web的,在项目名称上右键,添 ...
- Js正则Replace方法
JS正则的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\s+)|(\s+$)" ...
- 使用BeanUtils封装数据时数据类型的转换
//获得表单数据 Map<String, String[]> properties = request.getParameterMap(); User user = new User(); ...
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- 架构实战项目心得(六):后台服务nosql数据库mongodb
一.架构介绍 mongodb有几种部署方式,这里采用的是副本集架构(Replica Set). 为了防止单点故障就需要引副本(Replication),当发生硬件故障或者其 ...