当前位置:网站首页>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)
边栏推荐
猜你喜欢

Literature reading: gopose 3D human pose estimation using WiFi

Techempower web framework performance test 21st round results release --asp Net core continue to move forward

Decision tree_ ID3_ C4.5_ CART

Convolutional Neural Networks in TensorFlow quizs on Coursera

day 1

How to export map files tutorial

Pay close attention! List of the latest agenda of 2022 open atom open source Summit
Siyuan notes V2.1.2 synchronization problem

Day 9 (this keyword and experiment)
Cmake series tutorial 2 HelloWorld
随机推荐
OpenGL learning (III) glut two-dimensional image rendering
PCIe link initialization & Training
Machine learning_ Softmax function (multi classification problem)
Tcl/tk file operation
[face to face experience of school recruitment] 8 real questions of pointer interview. Come and test how many you have mastered.
Pyhanlp installation tutorial
LSTM and Gru of RNN_ Attention mechanism
OpenGL learning (V) modern OpenGL triangle rendering
How to export map files tutorial
Techempower web framework performance test 21st round results release --asp Net core continue to move forward
Biopharmaceutical safety, power supply and production guarantee
PCI express physical layer - electrical part
Day 10 (inheritance, rewriting and use of super)
Calendar common methods
Software core data protection solution
PostgreSQL Elementary / intermediate / advanced certification examination (7.16) passed the candidates' publicity
Oneinstack installation and configuration PHP 8.1 and MySQL 8.0-oneinstack site building novice tutorial
Profile environment switching
Nftscan and port3 have reached strategic cooperation in the field of NFT data
OpenGL learning (II) opengl rendering pipeline