LeetCode·每日一题·1800.最大升序子数组和·模拟
2023-09-27 14:26:29 时间
链接:https://leetcode.cn/problems/maximum-ascending-subarray-sum/solution/-by-xun-ge-v-pj9c/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目![](https://img-blog.csdnimg.cn/3e90cc1709804c60bf292439cbd32ff3.png)
示例![](https://img-blog.csdnimg.cn/a62bcd836a1844adae665ac8760b43e2.png)
思路
题意 -> 在一个正整数组成的数组 nums 中,返回 nums 中一个 升序 子数组的最大可能元素和
返回值有两个限制条件:
- 返回值是子数组,不是子序列,子数组相当于是子串不能分割选择,而子序列是可以分割的可以随意选择
- 返回值是升序,要求子数组中前一个元素比当前元素小
按照这两个条件进行模拟即可。可以使用栈模拟,也可以使用双指针记录每一个子数组中的和
代码
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int maxAscendingSum(int* nums, int numsSize){
int max = nums[0];
int sum = nums[0];//记录最大值
for(int i = 1; i < numsSize; i++)//枚举每一个元素
{
if(nums[i] <= nums[i-1])//当前为分割点重置和
sum = 0;
sum += nums[i];
max = MAX(max, sum);//每次保存最大值
}
return max;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/maximum-ascending-subarray-sum/solution/-by-xun-ge-v-pj9c/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章
- 【LeetCode】有效的数独 [M](模拟)
- 【LeetCode】给表达式添加运算符 [H](递归)
- 【LeetCode】跳跃游戏 II [M](模拟)
- 【LeetCode】最大的以 1 为边界的正方形 [M](模拟)
- LeetCode_模拟_中等_498.对角线遍历
- LeetCode刷题(18)【中等】删除链表的倒数第 N 个结点(C++)
- LeetCode·每日一题·1017. 负二进制转换·模拟
- LeetCode·每日一题·2367. 算术三元组的数目·模拟
- LeetCode·每日一题·1144.递减元素使数组呈锯齿状·模拟
- LeetCode·每日一题·2023.至少在两个数组中出现的值·模拟
- LeetCode·每日一题·1684.统计一致字符串的数目·模拟
- LeetCode·每日一题·1106.解析布尔表达式·栈模拟
- LeetCode·每日一题·1662.检查两个字符串数组是否相等·模拟
- LeetCode·每日一题·1822.数组元素积的符号·模拟
- LeetCode·每日一题·1768.交替合并字符串·模拟
- LeetCode·每日一题·921.使括号有效的最少添加·栈模拟
- LeetCode·每日一题·面试题 01.08. 零矩阵· 模拟
- LeetCode·每日一题·788.旋转数字·模拟
- LeetCode·每日一题·1582.二进制矩阵中的特殊位置·模拟
- LeetCode·304竞赛·6132·使数组中所有元素都等于零·模拟·哈希
- LeetCode·54·59·885·2326.螺旋矩阵四件套·模拟
- LeetCode·每日一题·1408.数组中的字符串匹配·模拟
- 【算法】动态规划 ③ ( LeetCode 62.不同路径 | 问题分析 | 自顶向下的动态规划 | 自底向上的动态规划 )
- [LeetCode] 505. The Maze II 迷宫 II