[leetcode]53. Maximum Subarray最大子数组和
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最大子数组和的更多相关文章
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode 53. Maximum Subarray最大子序和 (C++)
题目: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
随机推荐
- Java StringBuffer和StringBuilder类
Java StringBuffer和StringBuilder类 当对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类. 和String类不同的是,StringBu ...
- ab压力测试nginx
centos7系统: yum install httpd-tools -y #安装ab压力测试工具
- django中多个app放入同一文件夹apps
开发IDE:pycharm 新建一个apps文件夹 需要整理的app文件夹拖到同一个文件夹中,即apps.(弹出对话框,取消勾选Search for references) 在pycharm 中,右键 ...
- Linux Capability探索实验
Linux内核从2.1版本开始,就开始支持Capabilities的安全机制.Capabilities安全机制提出的目的在于实现系统特权操作的更加细粒度的访问控制,使用户能够根据实际的安全需求来控制r ...
- httpd基础知识
apache简介 Apache取自"a patchy server"的读音,意思是充满补丁的服务器.Apache起初由伊利诺伊大学香槟分校的国家超级电脑应用中心(NCSA)开发 ...
- 前段开发神奇webstorm安装注册和汉化
软件下载地址: http://www.jetbrains.com/webstorm/ 安装完后退出. 重新打开,进行激活 这里我们选择“license server”然后输入:http://idea. ...
- Python的xml模块
先来一段xml代码 <?xml version="1.0"?> <data> <country name="Liechtenstein&qu ...
- Python的Django
1 第一部分目录详解 修改django的项目当中的url中的配置: from django.contrib import admin from django.conf.urls import ur ...
- Python数字(Number)
Python 数字数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变数字数据类型得值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象将被创建:var1 = 1var2 ...
- git之sourceTree操作流程
1x.sourceTree的使用流程 12.Git管理工具对比(GitBash.EGit.SourceTree) 11.SourceTree使用SSH克隆码云项目 ====== 1x.source ...