{"id":8212,"date":"2021-03-06T22:32:26","date_gmt":"2021-03-07T06:32:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8212"},"modified":"2021-03-06T22:34:06","modified_gmt":"2021-03-07T06:34:06","slug":"leetcode-1785-minimum-elements-to-add-to-form-a-given-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1785-minimum-elements-to-add-to-form-a-given-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1785. Minimum Elements to Add to Form a Given Sum"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;and two integers&nbsp;<code>limit<\/code>&nbsp;and&nbsp;<code>goal<\/code>. The array&nbsp;<code>nums<\/code>&nbsp;has an interesting property that&nbsp;<code>abs(nums[i]) &lt;= limit<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of elements you need to add to make the sum of the array equal to&nbsp;<\/em><code>goal<\/code>. The array must maintain its property that&nbsp;<code>abs(nums[i]) &lt;= limit<\/code>.<\/p>\n\n\n\n<p>Note that&nbsp;<code>abs(x)<\/code>&nbsp;equals&nbsp;<code>x<\/code>&nbsp;if&nbsp;<code>x &gt;= 0<\/code>, and&nbsp;<code>-x<\/code>&nbsp;otherwise.<\/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 = [1,-1,1], limit = 3, goal = -4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> You can add -2 and -3, then the sum of the array will be 1 - 1 + 1 - 2 - 3 = -4.\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,-10,9,1], limit = 100, goal = 0\n<strong>Output:<\/strong> 1\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;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= limit &lt;= 10<sup>6<\/sup><\/code><\/li><li><code>-limit &lt;= nums[i] &lt;= limit<\/code><\/li><li><code>-10<sup>9<\/sup>&nbsp;&lt;= goal &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<p>Compute the diff = abs(sum(nums) &#8211; goal)<br>ans = \uff08diff + limit &#8211; 1)) \/ limit<\/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 minElements(vector<int>& nums, int limit, int goal) {\n    int64_t diff = abs(goal - \n                       accumulate(begin(nums), end(nums), 0LL));\n    return (diff + limit - 1) \/ limit;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums&nbsp;and two integers&nbsp;limit&nbsp;and&nbsp;goal. The array&nbsp;nums&nbsp;has an interesting property that&nbsp;abs(nums[i]) &lt;= limit. Return&nbsp;the minimum number of elements you need to add&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177],"class_list":["post-8212","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8212","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=8212"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8212\/revisions"}],"predecessor-version":[{"id":8217,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8212\/revisions\/8217"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}