{"id":8263,"date":"2021-03-21T22:23:26","date_gmt":"2021-03-22T05:23:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8263"},"modified":"2021-03-21T22:23:45","modified_gmt":"2021-03-22T05:23:45","slug":"leetcode-1800-maximum-ascending-subarray-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1800-maximum-ascending-subarray-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1800. Maximum Ascending Subarray Sum"},"content":{"rendered":"\n<p>Given an array of positive integers&nbsp;<code>nums<\/code>, return the&nbsp;<em>maximum possible sum of an&nbsp;<strong>ascending<\/strong>&nbsp;subarray in&nbsp;<\/em><code>nums<\/code>.<\/p>\n\n\n\n<p>A subarray is defined as a contiguous sequence of numbers in an array.<\/p>\n\n\n\n<p>A subarray&nbsp;<code>[nums<sub>l<\/sub>, nums<sub>l+1<\/sub>, ..., nums<sub>r-1<\/sub>, nums<sub>r<\/sub>]<\/code>&nbsp;is&nbsp;<strong>ascending<\/strong>&nbsp;if for all&nbsp;<code>i<\/code>&nbsp;where&nbsp;<code>l &lt;= i &lt; r<\/code>,&nbsp;<code>nums<sub>i&nbsp;<\/sub>&lt; nums<sub>i+1<\/sub><\/code>. Note that a subarray of size&nbsp;<code>1<\/code>&nbsp;is&nbsp;<strong>ascending<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [10,20,30,5,10,50]\n<strong>Output:<\/strong> 65\n<strong>Explanation: <\/strong>[5,10,50] is the ascending subarray with the maximum sum of 65.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [10,20,30,40,50]\n<strong>Output:<\/strong> 150\n<strong>Explanation: <\/strong>[10,20,30,40,50] is the ascending subarray with the maximum sum of 150.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [12,17,15,13,10,11,12]\n<strong>Output:<\/strong> 33\n<strong>Explanation: <\/strong>[10,11,12] is the ascending subarray with the maximum sum of 33.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [100,10,1]\n<strong>Output:<\/strong> 100\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Running sum with resetting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<p>Track the running sum and reset it to zero if nums[i] &lt;= nums[i &#8211; 1]<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxAscendingSum(vector<int>& nums) {    \n    int ans = 0;\n    for (int i = 0, s = 0; i < nums.size(); ++i) {\n      if (i &#038;&#038; nums[i] <= nums[i - 1])\n        s = 0;\n      ans = max(ans, s += nums[i]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of positive integers&nbsp;nums, return the&nbsp;maximum possible sum of an&nbsp;ascending&nbsp;subarray in&nbsp;nums. A subarray is defined as a contiguous sequence of numbers in an&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,200],"class_list":["post-8263","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8263"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8263\/revisions"}],"predecessor-version":[{"id":8265,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8263\/revisions\/8265"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}