当前位置:网站首页>【LeetCode】10. Maximum subarray · maximum subarray sum
【LeetCode】10. Maximum subarray · maximum subarray sum
2022-07-18 15:20:00 【AQin1012】
Title Description
Description in English
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. A subarray is a contiguous part of an array.
English address
https://leetcode.com/problems/maximum-subarray/
Description of Chinese version
Give you an array of integers nums , Please find a continuous subarray with the largest sum ( A subarray contains at least one element ), Return to its maximum and . Subarray Is a continuous part of the array .
Example 1: Input :nums = [-2,1,-3,4,-1,2,1,-5,4] Output :6 explain : Continuous subarray [4,-1,2,1] And the biggest , by 6 .
Example 2: Input :nums = [1] Output :1 Example 3: Input :nums = [5,4,-1,7,8] Output :23
Tips :
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Address of Chinese version
https://leetcode.cn/problems/maximum-subarray/
Their thinking
If you encounter a positive number, add , Minus , If the result is less than 0 Then remove all the previous
How to solve the problem
The official version will be released first this time (˶‾᷄ ⁻̫ ‾᷅˵) There is only one truth ... I didn't do it (╥﹏╥)
Official edition
Method 1: Dynamic programming

class Solution {
public class Status {
public int lSum, rSum, mSum, iSum;
public Status(int lSum, int rSum, int mSum, int iSum) {
this.lSum = lSum;
this.rSum = rSum;
this.mSum = mSum;
this.iSum = iSum;
}
}
public int maxSubArray(int[] nums) {
return getInfo(nums, 0, nums.length - 1).mSum;
}
public Status getInfo(int[] a, int l, int r) {
if (l == r) {
return new Status(a[l], a[l], a[l], a[l]);
}
int m = (l + r) >> 1;
Status lSub = getInfo(a, l, m);
Status rSub = getInfo(a, m + 1, r);
return pushUp(lSub, rSub);
}
public Status pushUp(Status l, Status r) {
int iSum = l.iSum + r.iSum;
int lSum = Math.max(l.lSum, l.iSum + r.lSum);
int rSum = Math.max(r.rSum, r.iSum + l.rSum);
int mSum = Math.max(Math.max(l.mSum, r.mSum), l.rSum + r.lSum);
return new Status(lSum, rSum, mSum, iSum);
}
} Method 2: Divide and conquer

summary
Dynamic programming , Memory search ( Recursion with memos / Pruning of recursive trees Pruning) , In fact, space for time (。・ω・。)ノ
边栏推荐
- GooglePhoto设置壁纸----壁纸裁剪界面配置
- Exception: Unexpected end of ZLIB input stream
- AcWing 3652. Maximum continuous subsequence dynamic programming
- 【成像】【8】太赫兹光学——波束耦合,高阶高斯波束模型
- 基于NvidiaGPU的AI模型结构优化
- Win10 timed running program
- 86触摸开关/台扇/空调/智能家居/家电等,低功耗高抗干扰3键3路3通触摸IC-VK3603 ESOP8,性能稳定,灵敏度可调
- watch时的value问题
- [untitled]
- RMAN detailed tutorial (I) -- basic command code
猜你喜欢
随机推荐
「TakinTalks」_ 故障频繁发生,如何做好系统稳定性?
Mipi c-phy popular science
澳大利亚规模领先的鞋业公司与Boomi合作以加快电子商务和转型步伐
Go 原生插件使用问题全解析
CUDA and cudnn installation tutorial (super detailed) Uninstalling CUDA, installing nsight visual studio edition of CUDA fails, and there is no CUDA option in vs2019+cuda11.1 new project
AcWing 3652. 最大连续子序列 动态规划
2022 R2 mobile pressure vessel filling test questions and answers
Boyun was selected as the representative manufacturer of Gartner China cloud management tool market guide
进程和线程的区别
【服务器数据恢复】IBM某型号存储RAID5数据恢复案例
Vulnhub-DC9学习笔记
RS485接口OSI模型的应用层
[daily training] 515 Find the maximum value in each tree row
小米手机安全卸载内置应用
架构基础篇
线程池的实现
ZYNQ PL中断脉冲多久可以被CPU捕获到
Exception: Unexpected end of ZLIB input stream
VS2019 16.8 “消失“的团队资源管理器
RMAN detailed tutorial (I) -- basic command code






![[imaging] [7] terahertz optics - optical elements and subsystems](/img/bc/8d1d442ed8dfeb77cc3e08eb7b22eb.jpg)

