1800:最大升序子数组和

This commit is contained in:
huangge1199 2021-06-07 11:29:24 +08:00
parent a3c22d7ec1
commit ef1a063b41
2 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,80 @@
//给你一个正整数组成的数组 nums 返回 nums 中一个 升序 子数组的最大可能元素和
//
// 子数组是数组中的一个连续数字序列
//
// 已知子数组 [numsl, numsl+1, ..., numsr-1, numsr] 若对所有 il <= i < rnumsi < 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
//
// Related Topics 双指针
// 👍 15 👎 0
package leetcode.editor.cn;
//1800:最大升序子数组和
public class MaximumAscendingSubarraySum{
public static void main(String[] args) {
//测试代码
Solution solution = new MaximumAscendingSubarraySum().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int maxAscendingSum(int[] nums) {
int max = 0;
int size = nums.length;
int sum = nums[0];
for (int i = 1; i < size; i++) {
if (nums[i] > nums[i - 1]) {
sum += nums[i];
} else {
max = Math.max(sum, max);
sum = nums[i];
}
}
max = Math.max(sum, max);
return max;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,48 @@
<p>给你一个正整数组成的数组 <code>nums</code> ,返回 <code>nums</code> 中一个 <strong>升序 </strong>子数组的最大可能元素和。</p>
<p>子数组是数组中的一个连续数字序列。</p>
<p>已知子数组 <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> ,若对所有 <code>i</code><code>l <= i < r</code><code>nums<sub>i </sub> < nums<sub>i+1</sub></code> 都成立,则称这一子数组为 <strong>升序</strong> 子数组。注意,大小为 <code>1</code> 的子数组也视作 <strong>升序</strong> 子数组。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [10,20,30,5,10,50]
<strong>输出:</strong>65
<strong>解释:</strong>[5,10,50] 是元素和最大的升序子数组,最大元素和为 65 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [10,20,30,40,50]
<strong>输出:</strong>150
<strong>解释:</strong>[10,20,30,40,50] 是元素和最大的升序子数组,最大元素和为 150 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [12,17,15,13,10,11,12]
<strong>输出:</strong>33
<strong>解释:</strong>[10,11,12] 是元素和最大的升序子数组,最大元素和为 33 。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>nums = [100,10,1]
<strong>输出:</strong>100
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>1 <= nums[i] <= 100</code></li>
</ul>
<div><div>Related Topics</div><div><li>双指针</li></div></div>\n<div><li>👍 15</li><li>👎 0</li></div>