Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

题意:

求最大子数组和

思路:

开一个一维dp

原理:任何数加负数肯定比原值小

1. dp[i] stands for max sum of subarray[0,i] (nums[i] must be used)

2. coz any negative will worsen final result.  supposing I am standing at current nums[i],  if previous sum dp[i-1]  > 0, it will benifit result, then we wrap dp[i-1] + nums[i] to dp[i]

otherwise, if dp[i-1] < 0, we only keep nums[i]

3. the  dp function will be :

dp[i] = dp[i-1] >0 ? dp[i-1] + num[i] : num[i]

code

 class Solution {
public int maxSubArray(int[] nums) {
// dp[i]: max sum of subarray from 0 to i
int[] dp = new int[nums.length];
// initiliaze
dp[0] = nums[0];
int result = nums[0]; for(int i = 1; i < nums.length; i++){
// negative value will worsen result, if dp[i-1] < 0, we only keep nums[i]
dp[i] = dp[i-1] > 0 ? dp[i-1] + nums[i] : nums[i];
// update max result
result = Math.max(result, dp[i]);
}
return result;
}
}

思路

优化空间为O(1)

Coz result is only related to previous sum.

We can use a variable to track previous sum.

代码

 public class MaximumSubarray {
public int maxSubArray(int[] nums) {
// initialize
int result = Integer.MIN_VALUE;
// reset it to 0 if it's less than 0.
int sum = 0;
for (int n : nums) {
// if sum < 0, we only keep n; if sum > 0, sum to be added to benefit result
sum = n + Math.max(sum, 0);
// update max result
result = Math.max(result, sum);
}
return result;
}
}

[leetcode]53. Maximum Subarray最大子数组和的更多相关文章

  1. [LeetCode] 53. Maximum Subarray 最大子数组

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  2. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  3. LeetCode 53. Maximum Subarray最大子序和 (C++)

    题目: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  4. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  5. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  6. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  7. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  8. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  9. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

随机推荐

  1. 添加一个pv到vg后,误删新加入的pv,报 pv * not found or rejected by a filter

    问题如下 将某一pv加入vg vgextend cl /dev/xvdb1 然后进入fdisk将xvdb1分区删掉,重新创建pv 使用lvdisplay报警告 [root@localhost ~]# ...

  2. day02-python与变量

    1.堆区开辟空间存放 变量值  2.将存放 变量值 空间的地址提供给栈区  3.栈区为变量名开辟空间存放提供来的地址 变量直接相互赋值 定义变量的优化机制 定义变量与重新赋值

  3. python中的is和==

    is和== Python中的对象包含三要素:id.type.value id方法的返回值就是对象的内存地址其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值 is 判断的是a对 ...

  4. Java面向对象 第2节 Scanner 类和格式化输出printf

    §Scanner 类 java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入. 1.创建 Scanner 对象的基本语法:Scanner s = ...

  5. buffers和cached的区别

    原文:https://www.cnblogs.com/kevingrace/p/5991604.html buffers和cached解释 ============================== ...

  6. tinycc update VERSION to 0.9.27

    TinyCC全称为Tiny C Compiler, 是微型c编译器,可在linux/win/平台上编译使用. 在用代码里面使用tcc当脚本,性能比lua还快,目前已有网游服务端使用TCC脚本提高性能. ...

  7. 搜狗浏览器总是打开123.sogou.com-记搜狗浏览器遭遇劫持一例

    昨日,因从网上下载了office2010破解工具,压缩包中有个文件为名为[office 2010激活工具\为保证永久激活,要先点击这个配置,再点击KMSELDIYI激活.exe],单击之后没有反应.后 ...

  8. ORACLE procedure 一个参数分解成多个字符一点建议

    测试时给什么变量就会生成什么变量, 但是在PROCEDURE时,你给的变量就会变成去掉包含字符q'/ /' 使用procedure splice添加字符串结果,是不包含q'/.删除时用的riqi赋值语 ...

  9. 删除文件夹下各级子目录中的.svn文件

    建立一个文本文件,取名为removeSvn.reg(扩展名由txt改为reg),内容如下 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHIN ...

  10. ef join查询

    temp = temp.OrderByDescending(s => s.CreateTime).Skip((param.PageIndex - ) * param.PageSize).Take ...