当前位置:网站首页>[dynamic programming simple question] leetcode 53. maximum subarray and
[dynamic programming simple question] leetcode 53. maximum subarray and
2022-07-27 03:03:00 【Wow, Kaka, negative, positive】
LeetCode 53. Maximum subarray and
I'm not autistic ! Come on !
https://leetcode.cn/problems/maximum-subarray/
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 .
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 .
Input :nums = [1]
Output :1
Input :nums = [5,4,-1,7,8]
Output :23
Refer to this up Solution method
https://www.bilibili.com/video/BV1XR4y1j7Lo/
Five steps for topic analysis
1. Design status
dp[i] Says to the first i The largest subarray and .
By the end of i The subarray ending with an integer is divided into two cases :
- And the
i-1A sub array ending in an integer - A separate By the end of
iAn integer as a subarray
give an example :[1, -3, 2]
- With
-3Is the ending subarray, and the maximum subarray sum ismax(1+(-3), -3) = -2 - With
2Is the ending subarray, and the maximum subarray sum ismax((1+(-3)+2), 2) = 2
2. Write the equation of state transition
dp[i] = max(dp[i-1]+nums[i], nums[i])
3. Set the initial state
dp[0] = nums[0]
4. Perform state transition
5. Return to the final solution
Solution
// Taking the maximum
int max(int a, int b) {
return a > b ? a : b;
}
int maxSubArray(int* nums, int numsSize){
int dp[100001];
// The initial state
dp[0] = nums[0];
int maxValue = nums[0];
for(int i = 1; i < numsSize; ++i) {
// Perform state transition
dp[i] = max(dp[i-1] + nums[i], nums[i]);
maxValue = max(dp[i], maxValue);
}
// Return to the final solution
return maxValue;
}
Solution(Python)
Use Python Realizing the rotation method is simpler
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
边栏推荐
- 制作ppt时间轴
- 听说你们面试都跪在了接口测试上?
- Kubernetes Dashboard 部署应用以及访问
- Information collection port scanning tool nmap instructions
- 八皇后编程实现
- 基于GoLang实现API短信网关
- 论构造函数的原型是谁
- Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
- [SQL简单题] LeetCode 627. 变更性别
- Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"
猜你喜欢

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩

B-树的应用以及添加和删除操作

云开发寝适闹钟微信小程序源码

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

Cs224w fall course - --- 1.1 why graphs?

函数栈帧详解

time模块: 时间戳、结构化时间、格式化时间的获取与相互转化

Invalid target distribution: solution for 17

Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice

Interrupt, signal, system call
随机推荐
Inftnews | "traffic + experience" white lining e Digital Fashion Festival leads the new changes of digital fashion
Goatgui invites you to attend a machine learning seminar
Pyqt5 use pyqtgraph to draw dynamic scatter chart
Shortcut keys commonly used in idea
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
Database read-write separation and database and table segmentation
软件测试相关试题知识点
系统安全测试要怎么做,详细来说说
Kubernetes dashboard deployment application and access
哈希表与一致性哈希的原理理解以及应用
Ubuntu基于docker的mysql主从数据库配置
Plato Farm全新玩法,套利ePLATO稳获超高收益
Lua函数之非全局函数
制作ppt时间轴
批量复制宝贝上传提示乱码,如何解决?
iNFTnews | GGAC联合中国航天ASES 独家出品《中国2065典藏版》
Is the low commission account opening of Galaxy Securities Fund reliable, reliable and safe
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
Concept of data asset management