当前位置:网站首页>Sword finger offer 42. maximum sum of continuous subarrays
Sword finger offer 42. maximum sum of continuous subarrays
2022-07-24 19:25:00 【nsq1101】
subject
Enter an integer array , One or more consecutive integers in an array form a subarray . Find the maximum sum of all subarrays .
The required time complexity is O(n).
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.
Tips :
1 <= arr.length <= 10^5
-100 <= arr[i] <= 100
Ideas
Dynamic programming :
- State definition
- State turns negative
- initialization
- recursive
- end
Program
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp = [0] *len(nums)
dp[0] = nums[0]
for i in range(1,len(nums)):
dp[i] = dp[i-1] + nums[i] if dp[i-1] >= 0 else nums[i]
return max(dp)
边栏推荐
- The ark compiler is coming. What about APK reinforcement
- DDR SDRAM board design guide
- Integer
- PostgreSQL Elementary / intermediate / advanced certification examination (7.16) passed the candidates' publicity
- FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)
- Profile environment switching
- Mysql database, de duplication, connection
- 文献阅读:GoPose 3D Human Pose Estimation Using WiFi
- How to convert the world coordinates of unity's camera into view matrix
- Configmanager of unity framework [JSON configuration file reading and writing]
猜你喜欢

Emergency lighting design of large stadiums and gymnasiums

Machine learning_ Data processing and model evaluation

拿捏C指针

Common methods of string class

Leetcode652 finding duplicate subtrees

Tcl/tk file operation

Reading notes: you only look once:unified, real time object detection
Cmake series tutorial 2 HelloWorld

Convolutional neural network CNN

What are the benefits of knowledge management in enterprises?
随机推荐
Mysql8.0 learning record 19 - Page segments and tablespaces
Tcl/tk grouping and replacement rules
2022 Hangzhou Electric Multi school first Dragon Slayer (dfs+ state compression)
Leetcode402 remove K digits
Wireshark simple filter rule
Cesium uses czml to implement dynamic routes
Chapter 4 compound type
DDR SDRAM board design guide
First knowledge database
Convolutional Neural Networks in TensorFlow quizs on Coursera
MySQL8.0学习记录20 - Trigger
文献阅读:GoPose 3D Human Pose Estimation Using WiFi
Cmake series tutorial 2 HelloWorld
MySQL index principle and query optimization "suggestions collection"
[understanding of opportunity-48]: Guiguzi - Chapter 7 - collect information in advance, make predictions and implementation plans in advance
Zooinspector Download
Convolutional neural network CNN
Compressed string
Leetcode652 finding duplicate subtrees
Install SSL Certificate in Litespeed web server