当前位置:网站首页>[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
边栏推荐
- South Korea may ban apple and Google from offering commission to developers, the first in the world
- 2.整理华子面经--2
- C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
- 阿里云回应用户注册信息泄露事件
- LeetCode-279-完全平方数
- The computer is out of power. How did I pass the terrible interview of Tencent cloud?
- 常用60类图表使用场景、制作工具推荐
- IOT 设计与开发
- Fastdfs mind map
- MySQL进阶之常用函数
猜你喜欢
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
Some grounded words
opencv应用——以图拼图
亚马逊和Epic将入驻,微软应用商城向第三方开放
MySQL基础合集
3-5通过XSS获取cookie以及XSS后台管理系统的使用
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
3-5 obtaining cookies through XSS and the use of XSS background management system
Deep analysis of C language pointer (Part I)
Problem brushing plan -- dynamic programming (III)
随机推荐
Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code
Tensorflow and pytorch middle note feature map size adjustment to achieve up sampling
Sklearn function: Train_ Test_ Split (split training set and test set)
Gsi-ecm digital platform for engineering construction management
【SDU Chart Team - Core】SVG属性类设计之枚举
Common problems in deploying projects with laravel and composer for PHP
Ubuntu 20 installing centernet
Explore ASP Net core read request The correct way of body
Tensorflow1. X and 2 How does x read those parameters saved in CKPT
浅谈数据库设计之三大范式
setInterval、setTimeout、requestAnimationFrame
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
Deep analysis of C language function
MySQL进阶之表的增删改查
Selenium 显示等待WebDriverWait
Problem brushing plan -- dynamic programming (IV)
Tensorflow realizes gradient accumulation, and then returns
Go limit depth traversal of files in directory
C, print the source program of beautiful bell triangle