当前位置:网站首页>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)
};
边栏推荐
- 超高效!Swagger-Yapi的秘密
- [NVIDIA development board] FAQ (updated from time to time)
- Hutool gracefully parses URL links and obtains parameters
- Modify the video name from the name mapping relationship in the table
- 电脑F1-F12用途
- LeetCode:124. 二叉树中的最大路径和
- Image,cv2读取图片的numpy数组的转换和尺寸resize变化
- LeetCode:剑指 Offer 42. 连续子数组的最大和
- Crash problem of Chrome browser
- Bottom up - physical layer
猜你喜欢

JVM performance tuning and practical basic theory - Part 1

UnsupportedOperationException异常
![[MySQL] lock](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
[MySQL] lock

swagger设置字段required必填

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

TP-LINK 企业路由器 PPTP 配置

堆排序详解

Current situation and trend of character animation

vb. Net changes with the window, scales the size of the control and maintains its relative position

The harm of game unpacking and the importance of resource encryption
随机推荐
移位运算符
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
Tcp/ip protocol
Deep analysis of C language data storage in memory
Browser thread
[embedded] print log using JLINK RTT
pytorch训练好的模型在加载和保存过程中的问题
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
软件卸载时遇到trying to use is on a network resource that is unavailable
The mysqlbinlog command uses
[MySQL] lock
Variable length parameter
swagger设置字段required必填
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
How to effectively conduct automated testing?
有效提高软件产品质量,就找第三方软件测评机构
[brush questions] top101 must be brushed in the interview of niuke.com
JVM 快速入门
C語言雙指針——經典題型
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)