{"id":6629,"date":"2020-04-18T11:17:06","date_gmt":"2020-04-18T18:17:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6629"},"modified":"2020-04-18T11:17:42","modified_gmt":"2020-04-18T18:17:42","slug":"leetcode-1413-minimum-value-to-get-positive-step-by-step-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1413-minimum-value-to-get-positive-step-by-step-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1413. Minimum Value to Get Positive Step by Step Sum"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>nums<\/code>, you start with an initial&nbsp;<strong>positive<\/strong>&nbsp;value&nbsp;<em>startValue<\/em><em>.<\/em><\/p>\n\n\n\n<p>In each iteration, you calculate the step by step sum of&nbsp;<em>startValue<\/em>&nbsp;plus&nbsp;elements in&nbsp;<code>nums<\/code>&nbsp;(from left to right).<\/p>\n\n\n\n<p>Return the minimum&nbsp;<strong>positive<\/strong>&nbsp;value of&nbsp;<em>startValue<\/em>&nbsp;such that the step by step sum is never less than 1.<\/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 = [-3,2,-3,4,2]\n<strong>Output:<\/strong> 5\n<strong>Explanation: <\/strong>If you choose startValue = 4, in the third iteration your step by step sum is less than 1.\n<strong>                step by step sum\n&nbsp;               startValue = 4 | startValue = 5 | nums\n<\/strong>&nbsp;                 (4 <strong>-3<\/strong> ) = 1  | (5 <strong>-3<\/strong> ) = 2    |  -3\n&nbsp;                 (1 <strong>+2<\/strong> ) = 3  | (2 <strong>+2<\/strong> ) = 4    |   2\n&nbsp;                 (3 <strong>-3<\/strong> ) = 0  | (4 <strong>-3<\/strong> ) = 1    |  -3\n&nbsp;                 (0 <strong>+4<\/strong> ) = 4  | (1 <strong>+4<\/strong> ) = 5    |   4\n&nbsp;                 (4 <strong>+2<\/strong> ) = 6  | (5 <strong>+2<\/strong> ) = 7    |   2\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 = [1,2]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Minimum start value should be positive. \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 = [1,-2,-3]\n<strong>Output:<\/strong> 5\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>-100 &lt;= nums[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix sum<\/strong><\/h2>\n\n\n\n<p>Find the minimum prefix sum, ans = &#8211; min(prefix_sum, 0) + 1<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(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 minStartValue(vector<int>& nums) {\n    int min_sum = 0;\n    int sum = 0;\n    for (int num : nums) {\n      sum += num;\n      min_sum = min(min_sum, sum);\n    }\n    return -min_sum + 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums, you start with an initial&nbsp;positive&nbsp;value&nbsp;startValue. In each iteration, you calculate the step by step sum of&nbsp;startValue&nbsp;plus&nbsp;elements in&nbsp;nums&nbsp;(from left to right).&#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":[222,376,200],"class_list":["post-6629","post","type-post","status-publish","format-standard","hentry","category-array","tag-easy","tag-on","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6629","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=6629"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6629\/revisions"}],"predecessor-version":[{"id":6631,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6629\/revisions\/6631"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}