当前位置:网站首页>【每日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;
}
}
边栏推荐
- Please ask me, the queries written in my database account for 99%. Is it better to use pay as you go mode or reservation mode?
- FS2K人脸素描属性识别
- Practice of curve replacing CEPH in Netease cloud music
- WPF video hard decoding, rendering and playing (no airspace) (support 4K, 8K and high frame rate video)
- 3. Caller 服务调用 - dapr
- Solve the problem that subcomponents will not be destroyed through setTimeout
- 高并发、高可用、弹性扩展,天翼云护航企业云上业务
- 【TcaplusDB知识库】TcaplusDB限制条件介绍
- 大促场景下,如何做好网关高可用防护
- 2019 CSP J2 entry group csp-s2 improvement group round 2 video and question solution
猜你喜欢

【尚硅谷与腾讯云官方合作】硅谷课堂项目视频发布

Azure Kinect Microsoft camera unity development summary

StackOverflow 2022 开发者报告:PostgreSQL 超越 MySQL !

LTspice 电路仿真入门

Curve 替换 Ceph 在网易云音乐的实践

Slim gain (sgain) introduction and code implementation -- missing data filling based on generated countermeasure network

基于DataWorks的时效仿真平台|得物技术

NOIP普及组2006-2018初赛 2019 CSP-J1 2020 CSP-J1 完善程序题

批量修改指定字符文件名 bat脚本

这个简单的小功能,半年为我们产研团队省下213个小时
随机推荐
offsetwidth\clientwidth\scrollwidth
Written interview algorithm classic - longest palindrome substring
Solve the problem that subcomponents will not be destroyed through setTimeout
【TcaplusDB知识库】TcaplusDB限制条件介绍
大型体育赛事与犯罪风险
Interview with wangyuntao of China Academy of information technology: digital and real integration enables the prosperity and development of cultural industry
运维-- 统一网关非常必要
GCC efficient graph revolution for joint node representationlearning and clustering
Noip popularization group 2006-2018 preliminary round 2019 csp-j1 2020 csp-j1 improvement program
从入门到精通|Yalmip+Cplex在电力系统中的应用(超棒,看不懂算我输,没有收获也算我输)
这个简单的小功能,半年为我们产研团队省下213个小时
[laravel] about the composer installation of laravel8
From five capabilities to "1+5+n", Huawei makes the transformation of government and enterprises more stable
LTspice 电路仿真入门
With high concurrency, high availability and elastic expansion, Tianyi cloud escorts Enterprise Cloud business
【TcaplusDB知识库】查看tcapdir目录服务器
visio 使用
LDD 知识整理
这个简单的小功能,半年为我们产研团队省下213个小时
6 - 字典