{"id":7239,"date":"2020-08-11T22:21:45","date_gmt":"2020-08-12T05:21:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7239"},"modified":"2020-08-11T22:22:12","modified_gmt":"2020-08-12T05:22:12","slug":"leetcode-1546-maximum-number-of-non-overlapping-subarrays-with-sum-equals-target","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1546-maximum-number-of-non-overlapping-subarrays-with-sum-equals-target\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>target<\/code>.<\/p>\n\n\n\n<p>Return the maximum number of&nbsp;<strong>non-empty<\/strong>&nbsp;<strong>non-overlapping<\/strong>&nbsp;subarrays such that the sum of values in each subarray is equal to&nbsp;<code>target<\/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,1,1,1], target = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>There are 2 non-overlapping subarrays [<strong>1,1<\/strong>,1,<strong>1,1<\/strong>] with sum equals to target(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,3,5,1,4,2,-9], target = 6\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>There are 3 subarrays with sum equal to 6.\n([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping.<\/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 = [-2,6,6,3,5,4,1,2,8], target = 10\n<strong>Output:<\/strong> 3\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> nums = [0,0,0], target = 0\n<strong>Output:<\/strong> 3\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;=&nbsp;10^5<\/code><\/li><li><code>-10^4 &lt;= nums[i] &lt;=&nbsp;10^4<\/code><\/li><li><code>0 &lt;= target &lt;= 10^6<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Sum + DP<\/strong><\/h2>\n\n\n\n<p>Use a hashmap index to record the last index when a given prefix sum occurs.<br>dp[i] := max # of non-overlapping subarrays of nums[0~i], nums[i] is not required to be included.<br>dp[i+1] = max(dp[i],  \/\/ skip nums[i]<br>                          dp[index[sum &#8211; target] + 1] + 1) \/\/ use nums[i] to form a new subarray<br>ans = dp[n]<\/p>\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++\">\nclass Solution {\npublic:\n  int maxNonOverlapping(vector<int>& nums, int target) {\n    const int n = nums.size();\n    vector<int> dp(n + 1, 0); \/\/ ans at nums[i];\n    unordered_map<int, int> index; \/\/ {prefix sum -> last_index}\n    index[0] = -1;    \n    int sum = 0;\n    for (int i = 0; i < n; ++i) {\n      sum += nums[i];\n      int t = sum - target;\n      dp[i + 1] = dp[i]; \n      if (index.count(t))\n        dp[i + 1] = max(dp[i + 1], dp[index[t] + 1] + 1);\n      index[sum] = i;      \n    }\n    return dp[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;nums&nbsp;and an integer&nbsp;target. Return the maximum number of&nbsp;non-empty&nbsp;non-overlapping&nbsp;subarrays such that the sum of values in each subarray is equal to&nbsp;target. Example 1: Input:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,82,177,635,41],"class_list":["post-7239","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hashtable","tag-medium","tag-non-overlapping","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7239","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=7239"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7239\/revisions"}],"predecessor-version":[{"id":7241,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7239\/revisions\/7241"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}