{"id":7675,"date":"2020-11-14T20:59:07","date_gmt":"2020-11-15T04:59:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7675"},"modified":"2020-11-14T21:04:15","modified_gmt":"2020-11-15T05:04:15","slug":"leetcode-1658-minimum-operations-to-reduce-x-to-zero","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1658-minimum-operations-to-reduce-x-to-zero\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1658. Minimum Operations to Reduce X to Zero"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>x<\/code>. In one operation, you can either remove the leftmost or the rightmost element from the array&nbsp;<code>nums<\/code>&nbsp;and subtract its value from&nbsp;<code>x<\/code>. Note that this&nbsp;<strong>modifies<\/strong>&nbsp;the array for future operations.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum number<\/strong>&nbsp;of operations to reduce&nbsp;<\/em><code>x<\/code>&nbsp;<em>to&nbsp;<strong>exactly<\/strong><\/em>&nbsp;<code>0<\/code>&nbsp;<em>if it&#8217;s possible<\/em><em>, otherwise, return&nbsp;<\/em><code>-1<\/code>.<\/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,4,2,3], x = 5\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The optimal solution is to remove the last two elements to reduce x to zero.\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 = [5,6,7,8,9], x = 4\n<strong>Output:<\/strong> -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> nums = [3,2,20,1,1,3], x = 10\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.\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;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= x &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution1: Prefix Sum + Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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  int minOperations(vector<int>& nums, int x) {\n    const int n = nums.size();\n    vector<int> l(n), r(n);\n    unordered_map<int, int> ml, mr;\n    ml[0] = mr[0] = -1;\n    for (int i = 0; i < n; ++i) {\n      l[i] = nums[i] + (i > 0 ? l[i - 1] : 0);      \n      r[i] = nums[n - i - 1] + (i > 0 ? r[i - 1]: 0);\n      ml[l[i]] = mr[r[i]] = i;\n    }\n    int ans = INT_MAX;\n    for (int i = 0; i < n; ++i) {\n      int s1 = x - l[i];\n      auto it1 = mr.find(s1);\n      if (it1 != mr.end()) ans = min(ans, i + 1 + it1->second + 1);\n      int s2 = x - r[i];\n      auto it2 = ml.find(s2);\n      if (it2 != ml.end()) ans = min(ans, i + 1 + it2->second + 1);      \n    }\n    return ans > n ? -1 : ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution2: Sliding Window<\/strong><\/h2>\n\n\n\n<p>Find the longest sliding window whose sum of elements equals sum(nums) &#8211; x<br>ans = n &#8211; window_size<\/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 minOperations(vector<int>& nums, int x) {\n    const int n = nums.size();\n    int target = accumulate(begin(nums), end(nums), 0) - x;    \n    int ans = INT_MAX;\n    for (int s = 0, l = 0, r = 0; r < n; ++r) {\n      s += nums[r];\n      while (s > target && l <= r) s -= nums[l++];\n      if (s == target) ans = min(ans, n - (r - l + 1));\n    }\n    return ans > n ? -1 : ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums&nbsp;and an integer&nbsp;x. In one operation, you can either remove the leftmost or the rightmost element from the array&nbsp;nums&nbsp;and subtract&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[82,177,97,200,215],"class_list":["post-7675","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-hashtable","tag-medium","tag-prefix","tag-prefix-sum","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7675","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=7675"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7675\/revisions"}],"predecessor-version":[{"id":7677,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7675\/revisions\/7677"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}