当前位置:网站首页>【每日3题(2)】最大升序子数组和
【每日3题(2)】最大升序子数组和
2022-06-28 16:38:00 【程序猿不脱发2】
题目:
给你一个正整数组成的数组 nums ,返回 nums 中一个 升序 子数组的最大可能元素和。
子数组是数组中的一个连续数字序列。
已知子数组 [numsl, numsl+1, …, numsr-1, numsr] ,若对所有 i(l <= i < r),numsi < numsi+1 都成立,则称这一子数组为 升序 子数组。注意,大小为 1 的子数组也视作 升序 子数组。
示例 1:
输入:nums = [10,20,30,5,10,50]
输出:65
解释:[5,10,50] 是元素和最大的升序子数组,最大元素和为 65 。
示例 2:
输入:nums = [10,20,30,40,50]
输出:150
解释:[10,20,30,40,50] 是元素和最大的升序子数组,最大元素和为 150 。
示例 3:
输入:nums = [12,17,15,13,10,11,12]
输出:33
解释:[10,11,12] 是元素和最大的升序子数组,最大元素和为 33 。
示例 4:
输入:nums = [100,10,1]
输出:100
提示:
1 <= nums.length <= 100
1 <= nums[i] <= 100
思路:
一次遍历, 记录升序的累加和及累加的最大值,如果发现不再升序,则重新计算累加和。
java代码:
class Solution {
public int maxAscendingSum(int[] nums) {
int sum = nums[0];
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if(nums[i]>nums[i-1]) {
sum+=nums[i];
}else {
sum = nums[i];
}
max = Math.max(max, sum);
}
return max;
}
}
边栏推荐
- Csp-j1 csp-s1 preliminary training plan and learning points in summer and September 2022
- 强化 WordPress 的 11 种有效方法
- 小新黑苹果声卡ID注入
- PMD-源代码分析器介绍
- C#/VB.NET 将PDF转为Excel
- Must the database primary key be self incremented? What scenarios do not suggest self augmentation? ByteDance experience sharing using Flink state 𞓜 afternoon tea with sauce issue 16
- 大促场景下,如何做好网关高可用防护
- Convolutional neural networks for machine learning -- an introduction to CNN
- Js中的Bom
- [force button] 977 Square of ordered array
猜你喜欢
基数排序——【常见排序法(2/8)】
General solution of island problems and DFS framework
这个简单的小功能,半年为我们产研团队省下213个小时
StackOverflow 2022 开发者报告:PostgreSQL 超越 MySQL !
批量修改指定字符文件名 bat脚本
岛屿类问题通用解法与DFS框架
Lucky draw animation - Carp jumps over the dragon's gate
MATLB|可视化学习(plot和bar)
关于接口测试自动化的总结与思考
Noip popularization group 2006-2018 preliminary round 2019 csp-j1 2020 csp-j1 improvement program
随机推荐
如何在网站上安装 WordPress
【Redis】2021/01/31 Redis的简单归纳 No.01
Slim gain (sgain) introduction and code implementation -- missing data filling based on generated countermeasure network
Interview with wangyuntao of China Academy of information technology: digital and real integration enables the prosperity and development of cultural industry
如何将你的 WordPress 网站置于维护模式
China energy integration and Tianyi cloud create an "energy brain"
【Hot100】2. Add two numbers
抓取手机端变体组合思路设想
Super automation and the future of network security
【Hot100】1. Sum of two numbers
【TcaplusDB知识库】TcaplusDB技术支持介绍
Have you ever encountered the error that the main key of this setting is consistent with the database?
Noip1998-2018 popularization group csp-j2 2019 2020 problem solving report and video
The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
强化 WordPress 的 11 种有效方法
Code implementation of gain (4) -- gap dataset missing data filling based on GaN (sequence) [improved version]
offsetwidth\clientwidth\scrollwidth
How to install WordPress on a web site
基数排序——【常见排序法(2/8)】
Design details of the full stack CRM development tool webclient UI workbench