{"id":6337,"date":"2020-02-16T21:20:54","date_gmt":"2020-02-17T05:20:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6337"},"modified":"2020-02-22T00:19:24","modified_gmt":"2020-02-22T08:19:24","slug":"eetcode-1354-construct-target-array-with-multiple-sums","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/eetcode-1354-construct-target-array-with-multiple-sums\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1354. Construct Target Array With Multiple Sums"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1354. Construct Target Array With Multiple Sums - \u5237\u9898\u627e\u5de5\u4f5c EP309\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/t9pa9kpb-wk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given an array of integers&nbsp;<code>target<\/code>. From a starting array,&nbsp;<code>A<\/code>&nbsp;consisting of all 1&#8217;s, you may perform the following procedure :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>let&nbsp;<code>x<\/code>&nbsp;be the sum of all elements currently in your array.<\/li><li>choose index&nbsp;<code>i<\/code>, such that&nbsp;<code>0 &lt;= i &lt; target.size<\/code>&nbsp;and set the value of&nbsp;<code>A<\/code>&nbsp;at index&nbsp;<code>i<\/code>&nbsp;to&nbsp;<code>x<\/code>.<\/li><li>You may repeat this procedure&nbsp;as many times as needed.<\/li><\/ul>\n\n\n\n<p>Return True if it is possible to construct the&nbsp;<code>target<\/code>&nbsp;array from&nbsp;<code>A<\/code>&nbsp;otherwise&nbsp;return False.<\/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> target = [9,3,5]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Start with [1, 1, 1] \n[1, 1, 1], sum = 3 choose index 1\n[1, 3, 1], sum = 5 choose index 2\n[1, 3, 5], sum = 9 choose index 0\n[9, 3, 5] Done\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> target = [1,1,1,2]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> Impossible to create target array from [1,1,1,1].\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> target = [8,5]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>N == target.length<\/code><\/li><li><code>1 &lt;= target.length&nbsp;&lt;= 5 * 10^4<\/code><\/li><li><code>1 &lt;= target[i] &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Backwards Simulation<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309.png\" alt=\"\" class=\"wp-image-6347\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-2.png\" alt=\"\" class=\"wp-image-6348\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-3.png\" alt=\"\" class=\"wp-image-6349\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-4.png\" alt=\"\" class=\"wp-image-6350\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-4.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-4-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-4-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-5.png\" alt=\"\" class=\"wp-image-6351\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-5.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-5-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1354-ep309-5-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Start with the largest number in the array, since it should be the sum of all previous numbers.<\/p>\n\n\n\n<p>[9,3,5] => [9 &#8211; (3+5), 3, 5] => [1, 3, 5] => [1, 3, 5 &#8211; (1+3)] => [1, 3, 1] => [1, 3 &#8211; (1+1), 1] => [1,1,1] done<\/p>\n\n\n\n<p>Time complexity: O(n + log(n*t)*logn)<br>Space complexity: O(n)<\/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  bool isPossible(vector<int>& target) {\n    priority_queue<int> q(begin(target), end(target));\n    long sum = accumulate(begin(target), end(target), 0LL);    \n    while (true) {      \n      int t = q.top(); q.pop();\n      sum -= t;\n      if (t == 1 || sum == 1) return true;      \n      if (t < sum || sum == 0 || t % sum == 0) return false;\n      t %= sum;\n      sum += t;\n      q.push(t);\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;target. From a starting array,&nbsp;A&nbsp;consisting of all 1&#8217;s, you may perform the following procedure : let&nbsp;x&nbsp;be the sum of all elements&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[559,217,179],"class_list":["post-6337","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-backwards","tag-hard","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6337","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=6337"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6337\/revisions"}],"predecessor-version":[{"id":6352,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6337\/revisions\/6352"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}