当前位置:网站首页>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)
};
边栏推荐
- 深度剖析C语言数据在内存中的存储
- 704 binary search
- Computer cleaning, deleted system files
- ESP8266-RTOS物联网开发
- 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)
- Beijing invitation media
- gcc动态库fPIC和fpic编译选项差异介绍
- Sublime text using ctrl+b to run another program without closing other runs
- 【ROS】usb_cam相机标定
- Function coritization
猜你喜欢

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures

TP-LINK 企业路由器 PPTP 配置

Process of obtaining the electronic version of academic qualifications of xuexin.com
![[brush questions] top101 must be brushed in the interview of niuke.com](/img/55/5ca957e65d48e19dbac8043e89e7d9.png)
[brush questions] top101 must be brushed in the interview of niuke.com

ESP8266-RTOS物联网开发

C語言雙指針——經典題型

MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object

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

Delay initialization and sealing classes

Unified ordering background interface product description Chinese garbled
随机推荐
LeetCode:221. 最大正方形
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
C語言雙指針——經典題型
China polyether amine Market Forecast and investment strategy report (2022 Edition)
【Nvidia开发板】常见问题集 (不定时更新)
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
Trying to use is on a network resource that is unavailable
China Light conveyor belt in-depth research and investment strategy report (2022 Edition)
torch建立的网络模型使用torchviz显示
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
sublime text的编写程序时的Tab和空格缩进问题
What are the common processes of software stress testing? Professional software test reports issued by companies to share
Research and investment forecast report of citronellol industry in China (2022 Edition)
Unsupported operation exception
Screenshot in win10 system, win+prtsc save location
ROS compilation calls the third-party dynamic library (xxx.so)
被破解毁掉的国产游戏之光
TP-LINK enterprise router PPTP configuration
marathon-envs项目环境配置(强化学习模仿参考动作)
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway