当前位置:网站首页>[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
边栏推荐
- UKFslam
- 引入结构化并发,Swift 5.5 发布!
- PHP的Laravel与Composer部署项目时常见问题
- On IRP from the perspective of source code
- 管道和xargs
- Express③(使用Express编写接口、跨域有关问题)
- 一些接地气的话儿
- 深入探究ASP.NET Core读取Request.Body的正确方式
- Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
猜你喜欢

Thinkphp5 + data large screen display effect

浅谈数据库设计之三大范式

Question brushing plan -- backtracking method (I)

What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer

Win 11K in 100 days, super complete learning guide for job transfer test

Arm architecture assembly instructions, registers and some problems

C, print the source program of beautiful bell triangle

MySQL基础合集

Problem brushing plan -- dynamic programming (III)

airbase 初步分析
随机推荐
C knowledge
Is qiniu school useful and is the recommended securities account safe
1.整理华子面经--1
Thread safe sigleton (singleton mode)
Problem brushing plan -- dynamic programming (IV)
pikachuxss如何获取cookie靶场,返回首页总是失败
Two Stage Detection
Selenium 显示等待WebDriverWait
2.整理华子面经--2
go reflect
Detailed explanation of basic assembly instructions of x86 architecture
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
Common problems in deploying projects with laravel and composer for PHP
Google 尝试在 Chrome 中使用 Rust
wait、waitpid
居家第二十三天的午饭
Fastdfs思维导图
[SDU chart team - core] enumeration of SVG attribute class design
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
Arm architecture assembly instructions, registers and some problems