当前位置:网站首页>[daily 3 questions (2)] maximum ascending subarray sum
[daily 3 questions (2)] maximum ascending subarray sum
2022-06-28 16:58:00 【Programmed ape without hair loss 2】
subject :
I'll give you an array of positive integers nums , return nums In a Ascending The maximum possible elements of the subarray and .
A subarray is a sequence of consecutive numbers in an array .
Known subarray [numsl, numsl+1, …, numsr-1, numsr] , If for all i(l <= i < r),numsi < numsi+1 All set up , This subarray is called Ascending Subarray . Be careful , The size is 1 The subarray of is also treated as Ascending Subarray .
Example 1:
Input :nums = [10,20,30,5,10,50]
Output :65
explain :[5,10,50] Is the element and the largest ascending subarray , The largest sum of elements is 65 .
Example 2:
Input :nums = [10,20,30,40,50]
Output :150
explain :[10,20,30,40,50] Is the element and the largest ascending subarray , The largest sum of elements is 150 .
Example 3:
Input :nums = [12,17,15,13,10,11,12]
Output :33
explain :[10,11,12] Is the element and the largest ascending subarray , The largest sum of elements is 33 .
Example 4:
Input :nums = [100,10,1]
Output :100
Tips :
1 <= nums.length <= 100
1 <= nums[i] <= 100
Ideas :
One traverse , Record the cumulative sum of ascending order and the maximum value of accumulation , If found no longer ascending , Then recalculate the cumulative sum .
java Code :
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;
}
}
边栏推荐
- [tcapulusdb knowledge base] view the business password
- 【世界海洋日】TcaplusDB号召你一同保护海洋生物多样性
- 这个简单的小功能,半年为我们产研团队省下213个小时
- LTspice 电路仿真入门
- Use open connector to integrate hubspot and SAP systems
- 2019 CSP J2入门组 CSP-S2提高组 第2轮 视频与题解
- PotPlayer播放百度雲盤視頻
- MATLB|电力系统优化运行与市场化
- Solve the problem that subcomponents will not be destroyed through setTimeout
- R 编程语言 - 简介
猜你喜欢

The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure

【Golang】安装 iris 的方法

Cloud sports, 360 ° witnessing speed and passion

Fs2k face sketch attribute recognition

QQ appears large-scale number theft, why is this? Is there no solution?

清华大佬耗时3天总结出的'常见网络协议汇总'

CRM 全栈开发工具 WebClient UI Workbench 的设计细节介绍

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

这个简单的小功能,半年为我们产研团队省下213个小时

visio 使用
随机推荐
这个简单的小功能,半年为我们产研团队省下213个小时
硕士学位论文写作探索学习
[random talk] January 31, 2021 Oh Huo
PotPlayer播放百度云盘视频
QQ appears large-scale number theft, why is this? Is there no solution?
Written interview algorithm classic - longest palindrome substring
如何备份 WordPress 数据库
apache 设置timeout参数
PMD-源代码分析器介绍
高并发、高可用、弹性扩展,天翼云护航企业云上业务
你好,现在网上炒股开户买股票安全吗?
解决sqoop出现 ERROR manager.SqlManager: Generic SqlManager.listDatabases() not implemented
On the design principle of price discount in SAP software
FS2K人脸素描属性识别
基于DataWorks的时效仿真平台|得物技术
DPDK 20.11编译安装运行程序
【杂谈】2021/01/31 哦豁
[golang] how to install iris
如何让你的 WordPress 网站更安全
Noip1998-2018 csp-s2 2019 2021 improvement group problem solving report and video