当前位置:网站首页>Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
2022-07-06 08:45:00 【Bertil】
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
Be careful : This topic and the main station 53 The question is the same :https://leetcode-cn.com/problems/maximum-subarray/
Their thinking
1. First define dp Array represents the largest subsequence ending with the current element and , Then traverse nums Array for each dp Element value , Finally, return the maximum value
2. State transition equation :dp[i] = Math.max(nums[i], dp[i - 1] + nums[i])
Code
/** * @param {number[]} nums * @return {number} */
var maxSubArray = function(nums) {
let dp = [nums[0]]
for(let i = 1; i < nums.length; i ++) {
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i])
}
return Math.max(...dp)
};
边栏推荐
- JVM 快速入门
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- Indentation of tabs and spaces when writing programs for sublime text
- China vanadium battery Market Research and future prospects report (2022 Edition)
- Marathon envs project environment configuration (strengthen learning and imitate reference actions)
- Generator parameters incoming parameters
- sys. argv
- R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
- China polyether amine Market Forecast and investment strategy report (2022 Edition)
- poi追加写EXCEL文件
猜你喜欢
The harm of game unpacking and the importance of resource encryption
JVM quick start
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
Sublime text using ctrl+b to run another program without closing other runs
Simple use of promise in uniapp
优秀的软件测试人员,都具备这些能力
What is CSRF (Cross Site Request Forgery)?
vb.net 随窗口改变,缩放控件大小以及保持相对位置
深度剖析C语言数据在内存中的存储
随机推荐
gcc动态库fPIC和fpic编译选项差异介绍
Light of domestic games destroyed by cracking
egg. JS directory structure
LeetCode:124. 二叉树中的最大路径和
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
[embedded] print log using JLINK RTT
hutool优雅解析URL链接并获取参数
C语言深度解剖——C语言关键字
egg. JS project deployment online server
[MySQL] lock
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
View computer devices in LAN
The mysqlbinlog command uses
vb. Net changes with the window, scales the size of the control and maintains its relative position
To effectively improve the quality of software products, find a third-party software evaluation organization
Bottom up - physical layer
生成器参数传入参数