当前位置:网站首页>[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
边栏推荐
- 【SDU Chart Team - Core】SVG属性类设计之枚举
- setInterval、setTimeout、requestAnimationFrame
- opencv应用——以图拼图
- Sequential state
- How to play the guiding role of testing strategy
- ubutnu20安裝CenterNet
- On IRP from the perspective of source code
- 学会打字后的思考
- Addition, deletion, modification and query of MySQL advanced table
- Detectron2 usage model
猜你喜欢

Lunch on the 23rd day at home

C, print the source program of beautiful bell triangle

Write table of MySQL Foundation (create table)

GSI-ECM工程建设管理数字化平台

Some grounded words

Deep analysis of C language pointer (Part I)

电脑越用越慢怎么办?文件误删除恢复方法

CUDA, NVIDIA driver, cudnn download address and version correspondence

Common problems in deploying projects with laravel and composer for PHP

Deep analysis of C language function
随机推荐
Go limit depth traversal of files in directory
Pytorch selects the first k maximum (minimum) values and their indexes in the data
Sequential state
Ubuntu 20 installing centernet
Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
thinkphp5+数据大屏展示效果
笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
MySQL数据库常识之储存引擎
South Korea may ban apple and Google from offering commission to developers, the first in the world
go slice
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
Linux中,MySQL的常用命令
wait、waitpid
[leetcode refers to offer 27. Image of binary tree (simple)]
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
Singleton mode
Tensorflow realizes gradient accumulation, and then returns
41. 缺失的第一个正数
Deep analysis of C language pointer (Part I)
Express③(使用Express编写接口、跨域有关问题)