当前位置:网站首页>[leetcode refers to offer 42. Maximum sum of continuous subarrays (simple)]
[leetcode refers to offer 42. Maximum sum of continuous subarrays (simple)]
2022-04-23 21:20:00 【Minaldo7】
subject :
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
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
Dynamic programming Divide and conquer method
class Solution {
public int maxSubArray(int[] nums) {
int dp[] = new int[nums.length];
dp[0] = nums[0];
int max = dp[0];
for(int i=1;i<nums.length;i++){
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
max = Math.max(dp[i], max);
}
return max;
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479457.html
边栏推荐
- The more you use the computer, the slower it will be? Recovery method of file accidental deletion
- 1.整理华子面经--1
- Rust更适合经验较少的程序员?
- On IRP from the perspective of source code
- MySQL basic collection
- Is qiniu school useful and is the recommended securities account safe
- Selenium displays webdriverwait
- Deep analysis of C language function
- Tensorflow1. X and 2 How does x read those parameters saved in CKPT
- Selenium 显示等待WebDriverWait
猜你喜欢
Reentrant function
airbase 初步分析
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
Question brushing plan - depth first search (II)
管道和xargs
MySQL进阶之数据的增删改查(DML)
Win 11K in 100 days, super complete learning guide for job transfer test
居家第二十三天的午饭
Opencv application -- jigsaw puzzle
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
随机推荐
On IRP from the perspective of source code
go interface
Detectron2 usage model
笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
Tensorflow and pytorch middle note feature map size adjustment to achieve up sampling
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
MySQL数据库常识之储存引擎
Recommended usage scenarios and production tools for common 60 types of charts
go defer
Express ③ (use express to write interface and cross domain related issues)
Thinking after learning to type
Leetcode-279-complete square number
go map
LeetCode-279-完全平方数
Is rust more suitable for less experienced programmers?
浅谈数据库设计之三大范式
PHP的Laravel与Composer部署项目时常见问题
MySQL basic collection
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
How to make Jenkins job run automatically after startup