当前位置:网站首页>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)
};
边栏推荐
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- [embedded] print log using JLINK RTT
- tree树的精准查询
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- JVM 快速入门
- 生成器参数传入参数
- Sort according to a number in a string in a column of CSV file
- C語言雙指針——經典題型
- Indentation of tabs and spaces when writing programs for sublime text
- JS inheritance method
猜你喜欢
深度剖析C语言指针
JS inheritance method
Precise query of tree tree
Image, CV2 read the conversion and size resize change of numpy array of pictures
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
深度剖析C语言数据在内存中的存储
Unsupported operation exception
Light of domestic games destroyed by cracking
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
Detailed explanation of heap sorting
随机推荐
torch建立的网络模型使用torchviz显示
On the inverse order problem of 01 knapsack problem in one-dimensional state
egg. JS getting started navigation: installation, use and learning
egg. JS directory structure
egg. JS project deployment online server
Navicat premium create MySQL create stored procedure
Shift Operators
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
Promise 在uniapp的简单使用
可变长参数
软件卸载时遇到trying to use is on a network resource that is unavailable
ROS compilation calls the third-party dynamic library (xxx.so)
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
【嵌入式】使用JLINK RTT打印log
LeetCode:387. 字符串中的第一个唯一字符
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
Sublime text using ctrl+b to run another program without closing other runs
电脑F1-F12用途
MySQL learning records 12jdbc operation transactions
gcc动态库fPIC和fpic编译选项差异介绍