[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.
这道题让求最大子数组之和,并且要用两种方法来解,分别是 O(n) 的解法,还有用分治法 Divide and Conquer Approach,这个解法的时间复杂度是 O(nlgn),那就先来看 O(n) 的解法,定义两个变量 res 和 curSum,其中 res 保存最终要返回的结果,即最大的子数组之和,curSum 初始值为0,每遍历一个数字 num,比较 curSum + num 和 num 中的较大值存入 curSum,然后再把 res 和 curSum 中的较大值存入 res,以此类推直到遍历完整个数组,可得到最大子数组的值存在 res 中,代码如下:
C++ 解法一:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int res = INT_MIN, curSum = ;
for (int num : nums) {
curSum = max(curSum + num, num);
res = max(res, curSum);
}
return res;
}
};
Java 解法一:
public class Solution {
public int maxSubArray(int[] nums) {
int res = Integer.MIN_VALUE, curSum = 0;
for (int num : nums) {
curSum = Math.max(curSum + num, num);
res = Math.max(res, curSum);
}
return res;
}
}
题目还要求我们用分治法 Divide and Conquer Approach 来解,这个分治法的思想就类似于二分搜索法,需要把数组一分为二,分别找出左边和右边的最大子数组之和,然后还要从中间开始向左右分别扫描,求出的最大值分别和左右两边得出的最大值相比较取最大的那一个,代码如下:
C++ 解法二:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if (nums.empty()) return ;
return helper(nums, , (int)nums.size() - );
}
int helper(vector<int>& nums, int left, int right) {
if (left >= right) return nums[left];
int mid = left + (right - left) / ;
int lmax = helper(nums, left, mid - );
int rmax = helper(nums, mid + , right);
int mmax = nums[mid], t = mmax;
for (int i = mid - ; i >= left; --i) {
t += nums[i];
mmax = max(mmax, t);
}
t = mmax;
for (int i = mid + ; i <= right; ++i) {
t += nums[i];
mmax = max(mmax, t);
}
return max(mmax, max(lmax, rmax));
}
};
Java 解法二:
public class Solution {
public int maxSubArray(int[] nums) {
if (nums.length == 0) return 0;
return helper(nums, 0, nums.length - 1);
}
public int helper(int[] nums, int left, int right) {
if (left >= right) return nums[left];
int mid = left + (right - left) / 2;
int lmax = helper(nums, left, mid - 1);
int rmax = helper(nums, mid + 1, right);
int mmax = nums[mid], t = mmax;
for (int i = mid - 1; i >= left; --i) {
t += nums[i];
mmax = Math.max(mmax, t);
}
t = mmax;
for (int i = mid + 1; i <= right; ++i) {
t += nums[i];
mmax = Math.max(mmax, t);
}
return Math.max(mmax, Math.max(lmax, rmax));
}
}
Github 同步地址:
https://github.com/grandyang/leetcode/issues/53
类似题目:
Best Time to Buy and Sell Stock
Longest Turbulent Subarray
参考资料:
https://leetcode.com/problems/maximum-subarray/
https://leetcode.com/problems/maximum-subarray/discuss/20211/Accepted-O(n)-solution-in-java
https://leetcode.com/problems/maximum-subarray/discuss/20193/DP-solution-and-some-thoughts
LeetCode All in One 题目讲解汇总(持续更新中...)
[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 ...
随机推荐
- MyBatis-Generator 用法介绍
”工欲善其事,必先利其器“,古人说的很对,虽然不能做一个单纯的”工具帝“,但是自己有合适的工具集真的很关键.以前认识一个做逆向工程的高手,有自己的”反马套装“,其实不外乎就是 OD . IDA .Sy ...
- go modules包管理
记录一下go工程迁移go modules的过程. go mod golang从1.11版本之后引入了包管理-go mod,并通过环境变量GO111MODULE 设置: 默认GO111MODULE 为a ...
- 解决MybatisPlus修改时空字段不修改问题
今天遇到了一个问题,在更新数据时,MybatisPlus不会进行修改属性为空的数据表字段. 解决办法: 只需要在实体类的属性上加一行注释即可 /** * 姓名 */ @TableField(fill ...
- IDEA设置方法参数列表类型自动提示
默认情况下,IDEA的提示不够完全,可以通过以下设置,将提示功能打开的更完善. 效果如下面俩图所示
- WPF TabItem设置Visibility隐藏Control内容
源自MSDN问题. 思路很简答: TabControl因为只显示TabItem的选择项的control. 所以单独的设置tabitem的control或者使用control的触发器都是不起作用的. 只 ...
- Zabbix图表中文乱码(包含Docker安装乱码)
目录 Zabbix 4.0 版本 Zabbix 3.0 版本 Zabbix 4.0 Docker 版本 图表乱码问题解决 文章github 地址: 点我 最近在看 Zabbix 4.0 版本的官方文档 ...
- ASP.NET Core: BackgroundService停止(StopAsync)后无法重新启动(StartAsync)的问题
这里的 BackgroundService 是指: Microsoft.Extensions.Hosting.BackgroundService 1. 问题复现 继承该BackgroundServic ...
- Python - 时间相关与计划任务
Python - 时间处理与定时任务 1.计算明天和昨天的日期 # 获取今天.昨天和明天的日期 # 引入datetime模块 import datetime #计算今天的时间 today = date ...
- python之三方库(冷门+热门)
AES加密库 pycryptodome
- Freemarker简单封装
Freemarker是曾经很流行的一个模板库,它是一种通用的模板库,不仅仅可以用来渲染html. 模板可以分为两类: 只能生成特殊类型文件的模板,如jinja.django.Thymeleaf.jad ...