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]> ...
随机推荐
- python3 md5
参考: https://docs.python.org/3/library/hashlib.html?highlight=hashlib#credits https://blog.csdn.net/w ...
- oracle--dump->buffer cache (dump 深入实践一)
1,dump 取值 ALTER SESSION SET EVENTS 'immediate trace name buffers level n'; 只转储buffer header. 在level ...
- 013-PaymentUtils工具类模板
package ${enclosing_package}; import java.io.UnsupportedEncodingException; import java.security.Mess ...
- OpenCV文本图像的旋转矫正
用户在使用Android手机拍摄过程中难免会出现文本图像存在旋转角度.这里采用霍夫变换.边缘检测等数字图像处理算法检测图像的旋转角度,并根据计算结果对输入图像进行旋转矫正. 首先定义一个结构元素,再通 ...
- MySQL优化--创建索引,以及怎样索引才会生效 (03)
1. 创建索引 (看这里) 2.索引在什么情况下才会起作用(重点)
- shell之“>/dev/null 2>&1” 详解
shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过 %> 的形式来定义输出,其中 %> 代表文件描述符 我们将这个命令组合:"& ...
- 快速上手:在CVM上安装Apache
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由一步 发表于云+社区专栏 介绍 Apache HTTP服务器是世界上使用最广泛的Web服务器.它提供了许多强大的功能,包括可动态加载的 ...
- Hibernate一对多关系操作
1.创建两个实体类. 一个实体类是商品类,另一个实体类是商品的分类类. 在一对多关系的两个实体中,在编写实体类时必须要遵循以下规则: (1)在一的那一方的实体中,必须要有一个私有的多那一方的实体对象属 ...
- .net core 第一篇选择开发工具和环境
.net core 已经发布三年了,社区也逐步成熟.作为微软阵营的一员,忙了一年年底抽点时间系统学习下.学习资料主要为以下为主: 1. https://docs.microsoft.com/zh-cn ...
- C++运行符重载、友元函数
Complex.h #pragma once #include <iostream> using namespace std; //表示一个复数 class Complex { priva ...