当前位置:网站首页>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)
边栏推荐
- Converter
- Profile environment switching
- Ebpf verifier
- Wireshark simple filter rule
- asp. Net core, C # summary about path
- Tcl/tk file operation
- [resolved] CVC datatype valid. 1.2.1: '' is not a valid value for 'ncname'.
- Convolutional Neural Networks in TensorFlow quizs on Coursera
- OpenGL learning (IV) glut 3D image rendering
- day 3
猜你喜欢

Hidden Markov model HMM
![[laser principle and application -6]:q switching element and Q drive circuit board](/img/30/e199b73fb9b0ad335f26f2378cfc45.png)
[laser principle and application -6]:q switching element and Q drive circuit board

Common problems of multithreading and concurrent programming (to be continued)

LTSpice software power settings

Database index: index is not a panacea

OpenGL learning (III) glut two-dimensional image rendering

Tencent Browser service TBS usage

Reading notes: you only look once:unified, real time object detection
![[question 39] special question for Niuke in-depth learning](/img/18/0e182f2c003ff5dd8ed3751c718d73.png)
[question 39] special question for Niuke in-depth learning

Analysis of the basic concept of digital warehouse
随机推荐
拿捏C指针
Detailed explanation of the relationship between MySQL tables
Modelarts, Pangu big model, ModelBox... Detailed explanation of Huawei cloud AI development production line
Why are there loopholes in the website to be repaired
OpenGL learning (III) glut two-dimensional image rendering
Ensure the health and safety of front-line construction personnel, and implement wrong time construction at Shenzhen construction site
Equals() method of object class
湖仓一体释放全量数据价值,SequoiaDB v5.2线上发布会重磅来袭
First knowledge database
How to select software dongle
mysql排序.按字段值排序
Hidden Markov model HMM
Clion configuring WSL tool chain
MySQL8.0学习记录19 - 页区段与表空间
[JVM learning 04] JMM memory model
Tupu software digital twin civil aviation flight networking, building a new business form of smart Civil Aviation
In the spring of domestic databases
Biopharmaceutical safety, power supply and production guarantee
Emergency lighting design of large stadiums and gymnasiums
Getaverse,走向Web3的远方桥梁