当前位置:网站首页>[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
边栏推荐
- LeetCode-279-完全平方数
- go struct
- 启牛学堂有用吗,推荐的证券账户是否安全
- 学会打字后的思考
- Is rust more suitable for less experienced programmers?
- Rust更适合经验较少的程序员?
- Sharpness difference (SD) calculation method of image reconstruction and generation domain index
- go reflect
- The iswow64process function determines the number of program bits
- Valueerror: invalid literal for int() with base 10 conversion error related to data type
猜你喜欢

thinkphp5+数据大屏展示效果

管道和xargs

3-5通过XSS获取cookie以及XSS后台管理系统的使用

Question brushing plan - depth first search (II)

Addition, deletion, modification and query of advanced MySQL data (DML)

On IRP from the perspective of source code

Linux中,MySQL的常用命令

Prim、Kruskal

Is rust more suitable for less experienced programmers?

Fastdfs思维导图
随机推荐
go defer
MySQL基础合集
Linux中,MySQL的常用命令
unity 功能扩展
一些接地气的话儿
ubutnu20安裝CenterNet
启牛学堂有用吗,推荐的证券账户是否安全
go slice
Explore ASP Net core read request The correct way of body
41. The first missing positive number
常用60类图表使用场景、制作工具推荐
South Korea may ban apple and Google from offering commission to developers, the first in the world
MySQL基础之写表(创建表)
Introduce structured concurrency and release swift 5.5!
setInterval、setTimeout、requestAnimationFrame
Question brushing plan - depth first search DFS (I)
Leetcode-279-complete square number
Keywords static, extern + global and local variables
Factory mode
Presto on spark supports 3.1.3 records