LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray
Title:
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4]
,
the contiguous subarray [2,3]
has the largest product = 6
.
对于Product Subarray,要考虑到一种特殊情况,即负数和负数相乘:如果前面得到一个较小的负数,和后面一个较大的负数相乘,得到的反而是一个较大的数,如{2,-3,-7},所以,我们在处理乘法的时候,除了需要维护一个局部最大值,同时还要维护一个局部最小值,由此,可以写出如下的转移方程式:
max_copy[i] = max_local[i]
max_local[i + 1] = Max(Max(max_local[i] * A[i], A[i]), min_local * A[i])
min_local[i + 1] = Min(Min(max_copy[i] * A[i], A[i]), min_local * A[i])
class Solution {
public:
int maxProduct(vector<int>& nums) {
int pmin = nums[];
int pmax = nums[];
int result = nums[];
for (int i = ; i < nums.size(); i++){
int t1= pmax * nums[i];
int t2= pmin * nums[i];
pmax = max(nums[i],max(t1,t2));
pmin = min(nums[i],min(t1,t2));
result = max(result,pmax);
}
return result;
}
};
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
,
the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
http://blog.csdn.net/joylnwang/article/details/6859677
http://blog.csdn.net/linhuanmars/article/details/21314059
class Solution{
public:
int maxSubArray(int A[], int n) {
int maxSum = A[];
int sum = A[];
for (int i = ; i < n; i++){
if (sum < )
sum = ;
sum += A[i];
maxSum = max(sum,maxSum);
}
return maxSum;
}
};
扩展:子序列之和最接近于0
先对数组进行累加,这样得到同样长度的数组,然后,对数组排序,对排序后的数组相邻的元素相减计算绝对值,并比较大小。
class Solution{
public:
vector<int> simple(vector<int> nums,int target){
int min_gap = INT_MAX;
int index_min ;
int index_max;
for (int i = ; i < nums.size(); i++){
int sum = ;
for (int j = i; j < nums.size(); j++){
sum += nums[j];
if (min_gap > abs(sum-target)){
min_gap = abs(sum-target);
index_min = i;
index_max = j;
}
}
}
vector<int> result(nums.begin()+index_min,nums.begin()+index_max+);
return result;
}
vector<int> choose(vector<int> nums, int target){
vector<pair<int,int> > addSums(nums.size());
addSums[] = make_pair(nums[],);
for (int i =; i < nums.size(); i++){
addSums[i] = make_pair(addSums[i-].first + nums[i],i);
}
sort(addSums.begin(),addSums.end());
int min_gap = INT_MAX;
int index = -;
for (int i = ; i < addSums.size(); i++){
int t = abs(addSums[i].first - addSums[i-].first);
if (min_gap > t){
min_gap = t;
index = i;
}
}
int index_min = min(addSums[index].second,addSums[index-].second);
int index_max = max(addSums[index].second,addSums[index-].second);
vector<int> result(nums.begin()+index_min+,nums.begin()+index_max+);
return result;
}
};
这种做法我没有想到如何扩展到任意的t上面
LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关的更多相关文章
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- [Swift]LeetCode152. 乘积最大子序列 | 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] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- Memcached 安装及配置
下载Memcached.exe 保存到c:\memcached 运行command: 输入 c:\memcached\memcached.exe -d install 回车,安装memcached s ...
- $('li','div') $('div li') $('div li')
$('div','li')是$(子,父),是从父节点里找子,而不是找li外面的div $('div , li')才是找所有的div和li,之间不存在父子关系 $('div li') 是找div里面所有 ...
- docker设置代理
在天朝使用docker需要FQ. 下面给出docker的代理方式: HTTP_PROXY=http://10.167.251.83:8080 docker -d
- linux上部署应用
1.编写traffic.sh 引入相关的jar包及java环境路径 2.crontab -e 加入: */10 * * * * cd /opt/sys/traffic_version/bin & ...
- Java Socket编程readLine返回null,read返回-1的条件
客户端正常关闭socket的时候,服务器端的readLine()方法会返回null,或者read()方法会返回-1
- Win7-其中的文件夹或文件已在另一个程序中打开
Win7-其中的文件夹或文件已在另一个程序中打开 如何解决Win7系统在删除或移动文件时提示,“操作无法完成,因为其中的文件夹或文件已在另一个程序中打开,请关闭该文件夹或文件,然后重试”. 步骤阅 ...
- cvc-elt.1: Cannot find the declaration of element---与spring 无关的schema 验证失败
晚上查了好久,都是spring 出这种问题的解决方式,终于查到为什么了. http://wakan.blog.51cto.com/59583/7218/ 转自这个人.. 多谢啦! 为了验证 XML 文 ...
- QAQ数论模板笔记√
#include <cstdio> using namespace std; long long gcd(long long a, long long b) { // (a, b) ret ...
- Debug过程中的mock (及display窗口的使用)
转载:http://m.blog.csdn.net/blog/u012516903/18004965 在debug的时候,有3个地方可以进行mock测试 测试代码如下: 1.使用display窗口 W ...
- iOS动画篇:UIView动画
iOS的动画效果一直都很棒很,给人的感觉就是很炫酷很流畅,起到增强用户体验的作用.在APP开发中实现动画效果有很多种方式,对于简单的应用场景,我们可以使用UIKit提供的动画来实现. UIView动画 ...