{"id":6912,"date":"2020-06-13T21:40:34","date_gmt":"2020-06-14T04:40:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6912"},"modified":"2020-06-14T19:32:24","modified_gmt":"2020-06-15T02:32:24","slug":"leetcode-1477-find-two-non-overlapping-sub-arrays-each-with-target-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1477-find-two-non-overlapping-sub-arrays-each-with-target-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1477. Find Two Non-overlapping Sub-arrays Each With Target Sum"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1477. Find Two Non-overlapping Sub-arrays Each With Target Sum - \u5237\u9898\u627e\u5de5\u4f5c EP335\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/AeIxJrzx9MU?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>arr<\/code>&nbsp;and an integer&nbsp;<code>target<\/code>.<\/p>\n\n\n\n<p>You have to find&nbsp;<strong>two non-overlapping sub-arrays<\/strong>&nbsp;of&nbsp;<code>arr<\/code>&nbsp;each with sum equal&nbsp;<code>target<\/code>. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is&nbsp;<strong>minimum<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum sum of the lengths<\/em>&nbsp;of the two required sub-arrays, or return&nbsp;<em><strong>-1<\/strong><\/em>&nbsp;if you cannot&nbsp;find such two sub-arrays.<\/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> arr = [3,2,2,4,3], target = 3\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 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> arr = [7,3,4,7], target = 7\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.\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> arr = [4,3,2,6,2,3,4], target = 6\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> We have only one sub-array of sum = 6.\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> arr = [5,5,4,4,5], target = 3\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> We cannot find a sub-array of sum = 3.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [3,1,1,1,5,1,2,1], target = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 1000<\/code><\/li><li><code>1 &lt;= target &lt;= 10^8<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window + Best so far<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1477-ep335.png\" alt=\"\" class=\"wp-image-6926\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1477-ep335.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1477-ep335-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1477-ep335-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1477-ep335-2.png\" alt=\"\" class=\"wp-image-6927\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1477-ep335-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1477-ep335-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1477-ep335-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>Use a sliding window to maintain a subarray whose sum is &lt;= target<\/li><li>When the sum of the sliding window equals to target, we found a subarray [s, e]<\/li><li>Update ans with it&#8217;s length + shortest subarray which ends before s.<\/li><li>We can use an array to store the shortest subarray which ends before s.<\/li><\/ol>\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 minSumOfLengths(vector<int>& arr, int target) {\n    constexpr int kInf = 1e9;\n    const int n = arr.size();\n    \/\/ min_lens[i] := min length of a valid subarray ends or before i.\n    vector<int> min_lens(n, kInf);\n    int ans = kInf;\n    int sum = 0;\n    int s = 0;\n    int min_len = kInf;\n    for (int e = 0; e < n; ++e) {\n      sum += arr[e];\n      while (sum > target) sum -= arr[s++];\n      if (sum == target) {       \n        const int cur_len = e - s + 1;\n        if (s > 0 && min_lens[s - 1] != kInf)\n          ans = min(ans, cur_len + min_lens[s - 1]);\n        min_len = min(min_len, cur_len);\n      }\n      min_lens[e] = min_len;\n    }    \n    return ans >= kInf ? -1 : ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;arr&nbsp;and an integer&nbsp;target. You have to find&nbsp;two non-overlapping sub-arrays&nbsp;of&nbsp;arr&nbsp;each with sum equal&nbsp;target. There can be multiple answers so you have to&#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":[88,177,215,41,492],"class_list":["post-6912","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-greedy","tag-medium","tag-sliding-window","tag-subarray","tag-target-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6912","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=6912"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6912\/revisions"}],"predecessor-version":[{"id":6928,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6912\/revisions\/6928"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}