当前位置:网站首页>LeetCode#53. Maximum subarray sum
LeetCode#53. Maximum subarray sum
2022-07-06 15:21:00 【Rufeng ZHHH】
subject :
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
This problem is still a dynamic programming problem , We can get before n The sum of the elements , Then calculate once , Finally get the maximum value .
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp=[0]*len(nums)
dp[0]=nums[0]
for i in range(1,len(nums)):
if dp[i-1]>0:
dp[i]=dp[i-1]+nums[i]
else:
dp[i]=nums[i]
return max(dp)
边栏推荐
- Servlet
- Mysql database (I)
- What are the software testing methods? Show you something different
- Mysql database (IV) transactions and functions
- Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of cobalt 2022-2028: Research Report on technology, participants, trends, market size and share
- 51 lines of code, self-made TX to MySQL software!
- Sleep quality today 81 points
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
猜你喜欢
UCORE LaB6 scheduler experiment report
What are the software testing methods? Show you something different
STC-B学习板蜂鸣器播放音乐2.0
MySQL数据库(一)
ucore lab2 物理内存管理 实验报告
Jupyter installation and use tutorial
Intensive learning notes: Sutton book Chapter III exercise explanation (ex17~ex29)
Brief description of compiler optimization level
Daily code 300 lines learning notes day 9
Want to change jobs? Do you know the seven skills you need to master in the interview software test
随机推荐
Common Oracle commands
MySQL transactions
Global and Chinese markets for complex programmable logic devices 2022-2028: Research Report on technology, participants, trends, market size and share
Rearrange spaces between words in leetcode simple questions
Pedestrian re identification (Reid) - Overview
Crawler series of learning while tapping (3): URL de duplication strategy and Implementation
MySQL数据库(四)事务和函数
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
Your wechat nickname may be betraying you
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
What is "test paper test" in software testing requirements analysis
线程及线程池
Leetcode notes - dynamic planning -day6
Interface test interview questions and reference answers, easy to grasp the interviewer
The most detailed postman interface test tutorial in the whole network. An article meets your needs
C4D quick start tutorial - Introduction to software interface
ArrayList set
Mysql database (V) views, stored procedures and triggers
Report on the double computer experiment of scoring system based on 485 bus
Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share