当前位置:网站首页>[动态规划简单题] LeetCode 53. 最大子数组和
[动态规划简单题] LeetCode 53. 最大子数组和
2022-07-27 00:21:00 【哇咔咔负负得正】
LeetCode 53. 最大子数组和
我不自闭!加油加油!
https://leetcode.cn/problems/maximum-subarray/
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组 是数组中的一个连续部分。
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
输入:nums = [1]
输出:1
输入:nums = [5,4,-1,7,8]
输出:23
参照该 up 的解法
https://www.bilibili.com/video/BV1XR4y1j7Lo/
题目分析五步走
1. 设计状态
dp[i] 表示以第 i 个整数结尾的子数组中的最大子数组和。
以第 i 个整数结尾的子数组分为两种情况:
- 和第
i-1个整数结尾的子数组相连 - 单独以第
i个整数作为子数组
举例:[1, -3, 2]
- 以
-3为结尾的子数组最大子数组和为max(1+(-3), -3) = -2 - 以
2为结尾的子数组最大子数组和为max((1+(-3)+2), 2) = 2
2. 写出状态转移方程
dp[i] = max(dp[i-1]+nums[i], nums[i])
3. 设定初始状态
dp[0] = nums[0]
4. 执行状态转移
5. 返回最终的解
Solution
// 取最大值
int max(int a, int b) {
return a > b ? a : b;
}
int maxSubArray(int* nums, int numsSize){
int dp[100001];
// 初始状态
dp[0] = nums[0];
int maxValue = nums[0];
for(int i = 1; i < numsSize; ++i) {
// 执行状态转移
dp[i] = max(dp[i-1] + nums[i], nums[i]);
maxValue = max(dp[i], maxValue);
}
// 返回最终解
return maxValue;
}
Solution(Python)
使用 Python 实现轮换法更简洁
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
pre, max_value = 0, nums[0]
for value in nums:
pre = max(pre + value, value)
max_value = max(max_value, pre)
return max_value
边栏推荐
- C language program compilation (preprocessing)
- Inftnews | "traffic + experience" white lining e Digital Fashion Festival leads the new changes of digital fashion
- How small programs help the new model of smart home ecology
- ansible系列之:不收集主机信息 gather_facts: False
- [untitled]
- Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
- 八皇后编程实现
- Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
- [redis] quick start
- 聊聊连接池和线程
猜你喜欢

F8 catch traffic, F9 catch rabbits, f10turttle

GoatGui邀你参加机器学习研讨班

setTimeout第一个参数应该注意的地方

Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换

用最原始的方法纯手工实现常见的 20 个数组方法

Time module: acquisition and mutual transformation of timestamp, structured time and formatted time

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

Cs224w fall course - --- 1.1 why graphs?

Web3.0 world knowledge system sharing - what is Web3.0

如何使用DevExpress WPF在WinUI中创建第一个MVVM应用程序?
随机推荐
Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
ArduinoUNO驱动RGB模块全彩效果示例
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
软件测试相关试题知识点
Talk about connection pools and threads
【Redis】快速入门
「软件测试」包装简历从这几点出发,直接提升通过率
素因子分解--C(gcc)--PTA
Kubeadmin到底做了什么?
Uni app wechat applet search keywords are displayed in red
What did kubedmin do?
Cloud development sleeping alarm clock wechat applet source code
Kubernetes Dashboard 部署应用以及访问
Is the low commission account opening of Galaxy Securities Fund reliable, reliable and safe
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
关于url编解码应该选用的函数
批量复制宝贝上传提示乱码,如何解决?
Lua函数之非全局函数