Max-Sum Subarray
来源:互联网 发布:淘宝400电话 编辑:程序博客网 时间:2024/05/17 23:10
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
.
11/1
scan the array and push each element to a stack, if the top is negative, simply push, otherwise, pop-add and then push;
in fact just need to keep the top, so simplify the algorithm
public class Solution { public int maxSubArray(int[] A) { int len = A.length; if(len == 0) return 0; int max_sum = A[0]; int top = A[0]; for(int i=1; i<len; i++){ int a = A[i]; if(top < 0){ top = a; }else{ top += a; } max_sum = Math.max(max_sum, top); max_sum = Math.max(max_sum, a); } return max_sum; }}
0 0
- Max-Sum Subarray
- Algorithm Arrays-3 Max Sum Contiguous Subarray
- Max Subarray
- Max subarray
- Subarray Sum
- Subarray Sum
- subarray sum
- Subarray Sum
- Subarray Sum
- Find Max Subarray
- Max product subarray
- Max Sum
- Max Sum
- Max Sum
- max sum
- Max Sum
- Max Sum
- MAX SUM
- 交叉编译Cubietruck的u-boot
- dfgdfg
- Binary Tree: Inorder Traversal
- fdgdfg
- iOS 常见错误, 失误
- Max-Sum Subarray
- 解决CentOS 6.5更新后无法联网的问题以及利用yum的 yum-versionlock 插件锁定某个软件版本不升级的方法
- 黑马程序员—C语言笔记—了解Mac的基本使用和C语言基础语法
- 设计实例对比:MySQL vs MongoDB
- Longest Substring without Repeating Characters
- [Leetcode]Interleaving String
- 9、congtiki系统开发-边界路由器PING通测试-虚拟机下串口资源确认篇
- 排序(1)--冒泡,简单选择,快速,简单插入
- C++ 静态static 变量在 cocos2d-x 里面使用误区