当前位置:网站首页>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)
边栏推荐
- Iterators and generators
- Eigen User Guide (Introduction)
- Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
- Knowledge that you need to know when changing to software testing
- [pytorch] simple use of interpolate
- Report on the double computer experiment of scoring system based on 485 bus
- [C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)
- Portapack application development tutorial (XVII) nRF24L01 launch B
- 软件测试面试回答技巧
- How to build a nail robot that can automatically reply
猜你喜欢
几款开源自动化测试框架优缺点对比你知道吗?
Réponses aux devoirs du csapp 7 8 9
Do you know the advantages and disadvantages of several open source automated testing frameworks?
Winter vacation daily question - maximum number of balloons
The maximum number of words in the sentence of leetcode simple question
软件测试面试要问的性能测试术语你知道吗?
Jupyter installation and use tutorial
Lab 8 文件系统
Threads et pools de threads
Stc-b learning board buzzer plays music
随机推荐
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
UCORE lab5 user process management experiment report
自动化测试你必须要弄懂的问题,精品总结
Express
软件测试需求分析之什么是“试纸测试”
UCORE lab1 system software startup process experimental report
Mysql database (III) advanced data query statement
Do you know the advantages and disadvantages of several open source automated testing frameworks?
Collection collection and map collection
ucore lab7
CSAPP shell lab experiment report
C4D quick start tutorial - creating models
Crawler series of learning while tapping (3): URL de duplication strategy and Implementation
The wechat red envelope cover designed by the object is free! 16888
Want to change jobs? Do you know the seven skills you need to master in the interview software test
JDBC introduction
Daily code 300 lines learning notes day 9
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
The minimum number of operations to convert strings in leetcode simple problem
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share