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. 查看linux系统CPU及内存配置

    总核数 = 物理CPU个数 X 每颗物理CPU的核数 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 查看物理CPU个数 cat /proc/cpuinfo| grep & ...

  2. oracle-ords

    oracle rest data service ORDS Perforce    adv. 一定,必须:必然地pagination    n. 标记页数:页码,分页Online documentat ...

  3. python设置路径值时为什么要输入r

    r:代表处理不转义现象 Python中,u表示unicode string,表示使用unicode进行编码,没有u表示byte string,类型是str,在没有声明编码方式时,默认ASCI编码.如果 ...

  4. git tag 常用操作

    1.获取最新tag(获取不到就多获取几次) git fetch origin  或者 git fetch origin <tagname> 2. checkout tag到本地分支(如果看 ...

  5. G2 绘制混合图例 demo

    G2 绘制混合图例 demo import G2 from '@antv/g2'; import DataSet from '@antv/data-set'; // G2 对数据源格式的要求,仅仅是 ...

  6. Markdown语法说明(转)

    Markdown语法说明(转) Markdown创始人John Gruber的语法说明 附上本文链接 NOTE: This is Simplelified Chinese Edition Docume ...

  7. xcopy命令总结

    xcopy命令总结1.拷贝多个文件和目录用xcopy /yhie或者xcopy /yhis命令,注意目标路径要以\结尾,例如:xcopy /yhie *.* e:\xxx\2.拷贝多个固定名字的文件用 ...

  8. iterator简单描述

    Item 26. Prefer iterator to const iterator, reverse_iterator, and const_reverse_iterator. 上面一段话,是< ...

  9. Python的socket

    第一部分socket的简单示例 服务器部分: """ Description: Author:Nod Date: Record: #------------------- ...

  10. 《从零玩转python+人工智能-3》网易云课堂王顺子

    #1.145——152节课25章——面向对象三大特性小案例 class Animal: def __init__(self,name,age=1): self.name = name self.age ...