当前位置:网站首页>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)
};
边栏推荐
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- Image,cv2读取图片的numpy数组的转换和尺寸resize变化
- R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
- TCP/IP协议
- ROS compilation calls the third-party dynamic library (xxx.so)
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- C language double pointer -- classic question type
- Colorlog combined with logging to print colored logs
- Swagger setting field required is mandatory
- Navicat Premium 创建MySql 创建存储过程
猜你喜欢

TP-LINK enterprise router PPTP configuration

Light of domestic games destroyed by cracking

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Excellent software testers have these abilities

Deep analysis of C language pointer

pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof

Roguelike游戏成破解重灾区,如何破局?

egg. JS getting started navigation: installation, use and learning

Cisp-pte practice explanation

LeetCode:124. 二叉树中的最大路径和
随机推荐
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
Deep anatomy of C language -- C language keywords
Research and investment forecast report of citronellol industry in China (2022 Edition)
企微服务商平台收费接口对接教程
China Light conveyor belt in-depth research and investment strategy report (2022 Edition)
MySQL learning record 10getting started with JDBC
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
704 二分查找
【MySQL】鎖
软件卸载时遇到trying to use is on a network resource that is unavailable
延迟初始化和密封类
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
The mysqlbinlog command uses
sublime text的编写程序时的Tab和空格缩进问题
Chrome浏览器的crash问题
游戏解包的危害及资源加密的重要性
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
poi追加写EXCEL文件
TP-LINK enterprise router PPTP configuration
JS inheritance method