当前位置:网站首页>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)
};
边栏推荐
- MySQL learning record 07 index (simple understanding)
- Bottom up - physical layer
- Excellent software testers have these abilities
- China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
- Deep analysis of C language pointer
- Cisp-pte practice explanation
- 可变长参数
- The network model established by torch is displayed by torch viz
- vb. Net changes with the window, scales the size of the control and maintains its relative position
- JS native implementation shuttle box
猜你喜欢
同一局域网的手机和电脑相互访问,IIS设置
Bottom up - physical layer
JS native implementation shuttle box
[MySQL] lock
win10系统中的截图,win+prtSc保存位置
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
Trying to use is on a network resource that is unavailable
Variable length parameter
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
[embedded] cortex m4f DSP Library
随机推荐
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
sublime text的编写程序时的Tab和空格缩进问题
[embedded] cortex m4f DSP Library
ESP8266-RTOS物联网开发
visdom可视化实现与检查介绍
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
To effectively improve the quality of software products, find a third-party software evaluation organization
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
[brush questions] top101 must be brushed in the interview of niuke.com
按位逻辑运算符
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
JS native implementation shuttle box
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
Is it safe to open an account in Zheshang futures?
sys. argv
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
查看局域网中电脑设备
Beijing invitation media
Restful API design specification
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台