当前位置:网站首页>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)
};
边栏推荐
- Computer cleaning, deleted system files
- How to effectively conduct automated testing?
- Modify the video name from the name mapping relationship in the table
- swagger设置字段required必填
- MySQL learning record 07 index (simple understanding)
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- Charging interface docking tutorial of enterprise and micro service provider platform
- Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
- Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
- 704 binary search
猜你喜欢
Screenshot in win10 system, win+prtsc save location
生成器参数传入参数
TP-LINK enterprise router PPTP configuration
MySQL learning record 10getting started with JDBC
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
win10系统中的截图,win+prtSc保存位置
Delay initialization and sealing classes
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
Visual implementation and inspection of visdom
电脑清理,删除的系统文件
随机推荐
JVM quick start
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
C语言双指针——经典题型
704 binary search
What are the common processes of software stress testing? Professional software test reports issued by companies to share
vb. Net changes with the window, scales the size of the control and maintains its relative position
China Light conveyor belt in-depth research and investment strategy report (2022 Edition)
Variable length parameter
电脑清理,删除的系统文件
What is CSRF (Cross Site Request Forgery)?
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
How to effectively conduct automated testing?
Charging interface docking tutorial of enterprise and micro service provider platform
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
The mysqlbinlog command uses
Simple use of promise in uniapp
【嵌入式】Cortex M4F DSP库
LeetCode:221. 最大正方形
[MySQL] lock
Swagger setting field required is mandatory