41. Maximum Subarray
Description
Given an array of integers, find a contiguous subarray which has the largest sum.
The subarray should contain at least one number.
Example
Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.
Challenge
Can you do it in time complexity O(n)?
这是一道求最大子串和的问题,想解决这个问题,我们需要考虑如下几种情况:
情况一:加上一个正数——那自然最好了,求知不得嘛。且慢,假如前几个数的和为负数,突然来了个正数,是把正数加入旧的队列中,还是这个正数自立门户呢
啥意思?例如,有如此序列 -1,3,5。。。。等等,你是直接从3开始呢,还是把-1这个拖油瓶带着?很明显,为了保证整个子串的值最大,应该从3开始,该思路
对应于下面代码中的注释一。
情况二:加上的数是一个负数——虽然说加上一个负数可能会变小,但有时候我们还是得加上的。例如:3, 4, -5, 6, 7.......虽然加上-5会变小,但是它后面有更大的数,
能让整个序列得和变大。但有时候,加上了一个负数真的会使整个值变小,例如:3,-2, 1 我就三个数,加上-2后,哪怕再加上个一,相对于原来的3来说,还是变小了。
这个时候,就要求我们定义两个变量了,一个来保存最终的结果(res),另一个来“大胆地尝试”(curSum),如果一不小心curSum>res了,则更新res的值。代码如下:
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(int[] nums) {
// write your code here
int curSum=0;
int res=Integer.MIN_VALUE;
for(int num:nums){
curSum=Math.max(curSum+num,num);//注释一
res=Math.max(curSum,res);
}
return res;
}
}
41. Maximum Subarray的更多相关文章
- [LintCode笔记了解一下]41.Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sum. 首先 當題目涉及到求最大最小值時,最 ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
随机推荐
- 【题解】洛谷P4281 [AHOI2008] 紧急集合(求三个点LCA)
洛谷P4281:https://www.luogu.org/problemnew/show/P4281 思路 答案所在的点必定是三个人所在点之间路径上的一点 本蒟蒻一开始的想法是:先求出2个点之间的L ...
- Web打印连续的表格,自动根据行高分页
拿到这个需求,我已经蛋碎了一地,经过N天的攻克,终于是把它搞定了,只是不知道会不会在某种情况下出现BUG.表示我心虚没有敢做太多的测试.... ---------------------------- ...
- 高考结束了,在门头沟有没有想学php建站的。
教你简单的html 教你文章后台管理 数据库管理及备份 编程工具的使用 如何找到你要学习的内容
- 【微信开发】微信开发模式 api 接口文档简介
微信公众平台分为订阅号和服务号,服务号提供9大接口,需要通过微信认证后才能使用这些接口.认证费用300元.下面是接口的大致介绍: 1. 语音识别:通过语音识别接口,用户发送的语音,将会同时给出语音识别 ...
- 学习笔记(1)centos7 下安装nginx
学习笔记(1)centos7 下安装nginx 这里我是通过来自nginx.org的nginx软件包进行安装的. 1.首先为centos设置添加nginx的yum存储库 1.通过vi命令创建一个rep ...
- 【解决】could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
在同一套环境中跑了很多个项目都是用 docker-compose的方式启动的,导致创建的自定义网络过多出现下面的报错 Error response from daemon: could not fin ...
- 使用 PlantUML 高效画图
PlantUML 是一种程序员看了就会爱上的画图方式:自然,高效. 支持快速绘制: 时序图 类图 用例图 活动图 状态图 等等 安装教程 Intellij IDEA中安装 & 使用PlantU ...
- QP-nano结构分析
QP-nano是QP的一个裁剪版本,是一个通用的.可移植的.超轻量级的事件驱动型框架.适用于像8051.PIC.AVR.MSP430.68HC01/11/12.R8C/Tiny等资源受限的8位和16位 ...
- tomcat搭建https服务(非自签发)
平时做自己的web demo基本上都是用http协议进行访问. 但是正式情况基本上都是https进行访问,所以掌握https的配置是很关键的. 需要准备的材料: 一台可以可以外网访问的远程服务器 to ...
- C语言实现左旋字符串
#include<stdio.h> #include<stdlib.h> #include<string.h> void left_rotate(char *str ...