【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 ...
随机推荐
- Android之Intent和Activity
Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了.Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助.在Android的官 ...
- 个人笔记-CSS
http://localhost:1081/sdfsdfs/config-browser/actionNames.action 超出容器文字隐藏 .hiddenoverflowtext { width ...
- Oracle:创建存储过程
1.无参存储过程 create or replace procedure test_procasv_total number(10);begin select count(*) into v_tot ...
- SpringMvc自动代理
自动配置的好处是不需要挨个 实现[org.springframework.aop.framework.ProxyFactoryBean] ,只需要 advisor 配置和 <bean id=&q ...
- php html_entity_decode使用总结
在处理网页字符串的时候,尤其是做爬虫类的应用时,经常会涉及到要处理的字符串中包含html标签,现在对这类字符串的处理做一个小的总结: 有时候获取到的字符串中有html标签,在入库的时候出于安全的考虑通 ...
- nginx - KeepAlive详细解释
最近工作中遇到一个问题,想把它记录下来,场景是这样的: 从上图可以看出,用户通过Client访问的是LVS的VIP, VIP后端挂载的RealServer是Nginx服务器. Client可以是浏览器 ...
- go with go
1, vim 安装vim-go 打造GOLANG 专用IDE golang和vim-go安装配置 2, 阅读图书 <Go语言实战> William Kennedy等, 李兆海 译 3,在线 ...
- Unity3D GUI中的图片尾随鼠标旋转脚本
var Mid : Texture2D; var mouse : Texture2D; //鼠标图片 var mousePs = Vector2.zero; //鼠标的位置 private var a ...
- tomcat+java 占cpu 调试【top命令应用】
原文出处:http://www.blogjava.net/hankchen 现象: 在tomcat中部署java的web应用程序,过一段时间后出现tomcat的java进程持续占用cpu高达100%, ...
- Ajax的跨域问题
•跨域问题概述 •出于安全考虑,浏览器不允许ajax跨域获取数据 •可以通过script的src加载js的方式传递数据 fn({"a":"1","b& ...