【LeetCode】053. 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.
题解:
遍历所有组合,更新最大和。
Solution 1 (TLE)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size(), result = nums[];
for(int i=; i<n; ++i) {
int tmp = nums[i];
result = max(tmp,result);
for(int j=i+; j<n; ++j) {
tmp += nums[j];
result = max(tmp,result);
}
}
return result;
}
};
可以看做动态规划的简略版,见Solution 5
Solution 2 ()
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size(), result = nums[], tmp = ;
for(int i=; i<n; ++i) {
tmp = max(tmp + nums[i], nums[i]);
result = max(result, tmp);
}
return result;
}
};
贪心算法:The idea is to find the largest difference between the sums when you summing up the array from left to right. The largest difference corresponds to the sub-array with largest sum
Solution 3 ()
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum = , minsum = , result = nums[], n = nums.size();
for(int i = ; i < n; i++) {
sum += nums[i];
if(sum - minsum > res) result = sum - minsum;
if(sum < minsum) minsum = sum;
}
return result;
}
};
分治法:
Solution 4 ()
DP算法:维护一个一维数组。
Solution 5 ()
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n,);//dp[i] means the maximum subarray ending with nums[i];
dp[] = nums[];
int max = dp[];
for(int i = ; i < n; i++){
dp[i] = nums[i] + (dp[i - ] > ? dp[i - ] : );
max = Math.max(max, dp[i]);
}
return max;
}
};
【LeetCode】053. Maximum Subarray的更多相关文章
- 【LeetCode】53. Maximum Subarray (2 solutions)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- 【Leetcode】53. Maximum Subarray
题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- 系统安全-Google authenticator
对于某些人来说,盗取密码会比你想象的更简单 以下任意一种常见的操作都可能让你的密码面临被盗的风险: 在多个网站上使用同一个密码 从互联网上下载软件 点击电子邮件中的链接 两步验证可以将别有用心的人阻 ...
- 解决opencv无法读AVI视频的问题
原文来自:http://blog.csdn.net/yeqiu712/article/details/6220030 其实AVI只是一个外壳.里面的东西可不一样的! 问题:为什么我的电脑支持AVI或者 ...
- ios 推送证书没有密钥 解决方案【转载】
注意事项: 1.keychains选择Login 2.2.在创建完CertificateSigningRequest.certSigningRequest可以看到Keys中该有你的私有秘钥 3.按文档 ...
- 01 http协议概念及工作流程
一:HTTP协议 重要性: 无论是以后用webserverice ,还是用rest做大型架构,都离不开对HTTP协议的认识. 甚至可以简化的说: webservice = http协议+XML Res ...
- PowerBuilder -- 其他
判断某键是否被按下 KeyDown ( keycode ) 继承问题 如果是 uf_1是函数呢 你在父类UO_1的uf_1里面 写了代码,只要在子类UO_2的uf_1写了代码,默认是覆盖(over ...
- Continuously Integrate
 Continuously Integrate David Bartlett THE Build AS A "Big BAng" EvEnT in project develop ...
- Dubbo服务集群,常见容错机制:failover ,failsafe,failfase ,failback,forking
http://blog.csdn.net/hongweigg/article/details/52925920 http://m.blog.csdn.net/article/details?id=51 ...
- mybatis 运算符转义收录
在ibatis配置文件写SQL语句的时候对于一些比如“<”,">","<>","&"," ' &q ...
- 我的Android进阶之旅------>真正在公司看几天代码的感触
仅以此文来回顾这一周我的工作情况,以及由此而触发的感想. ============================================================= 来到新公司5天了, ...
- 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5 ,PHP5.6)支持。
本人预装了一个wamp的集成环境(Apache 2.4.9 + PHP 5.5.12 + mysql 5.6.17),今天在wamp环境下 添加PHP多版本 (PHP 5.5.30).中间两个过程,1 ...