{"id":2297,"date":"2018-03-22T20:30:29","date_gmt":"2018-03-23T03:30:29","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2297"},"modified":"2018-03-22T22:02:38","modified_gmt":"2018-03-23T05:02:38","slug":"leetcode-560-subarray-sum-equals-k","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-560-subarray-sum-equals-k\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 560. Subarray Sum Equals K"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 560. Subarray Sum Equals K - \u5237\u9898\u627e\u5de5\u4f5c EP176\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/mKXIH9GnhgU?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><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u95ee\u6709\u591a\u5c11\u5b50\u6570\u7ec4\u7684\u548c\u4e3ak\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/subarray-sum-equals-k\/description\/\">https:\/\/leetcode.com\/problems\/subarray-sum-equals-k\/description\/<\/a><\/p>\n<div class=\"question-description\">\n<p>Given an array of integers and an integer\u00a0<b>k<\/b>, you need to find the total number of continuous subarrays whose sum equals to\u00a0<b>k<\/b>.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>nums = [1,1,1], k = 2\r\n<b>Output:<\/b> 2\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of the array is in range [1, 20,000].<\/li>\n<li>The range of numbers in the array is [-1000, 1000] and the range of the integer\u00a0<b>k<\/b>\u00a0is [-1e7, 1e7].<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2306\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/560-ep176.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/560-ep176.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/560-ep176-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/560-ep176-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution -1: Brute Force<\/strong><\/h1>\n<p>For every pair of i,j, check sum(nums[i:j]) in O(j-i) = O(n)<\/p>\n<p>Time complexity: O(n^3) TLE<\/p>\n<p>Space complexity: O(1)<\/p>\n<h1><strong>Solution 0: Brute Force + Prefix sun<\/strong><\/h1>\n<p>Precompute the prefix sum and check sum of nums[i:j] in O(1)<\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 490 ms\r\nclass Solution {\r\npublic:\r\n  int subarraySum(vector&lt;int&gt;&amp; nums, int k) {\r\n    const int n = nums.size();\r\n    vector&lt;int&gt; sums(n + 1, 0);    \r\n    for (int i = 1; i &lt;= n; ++i)\r\n      sums[i] = sums[i - 1] + nums[i - 1];\r\n    int ans = 0;\r\n    for (int i = 0; i &lt; n; ++i)\r\n      for (int j = i; j &lt; n; ++j)\r\n        if (sums[j + 1] - sums[i] == k) ++ans;\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div>\n<h1><strong>Solution 1: Running Prefix sum<\/strong><\/h1>\n<p>Keep tracking the prefix sums and their counts.<\/p>\n<p>s -&gt; count: how many arrays nums[0:j] (j &lt; i) that has sum of s<\/p>\n<p>cur_sum = sum(nums[0:i])<\/p>\n<p>check how many arrays nums[0:j] (j &lt; i) that has sum (cur_sum &#8211; k)<\/p>\n<p>then there are the same number of arrays nums[j+1: i] that have sum k.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 36 ms\r\nclass Solution {\r\npublic:\r\n  int subarraySum(vector&lt;int&gt;&amp; nums, int k) {\r\n    if (nums.empty()) return 0;\r\n    unordered_map&lt;int, int&gt; counts{{0,1}};\r\n    int cur_sum = 0;\r\n    int ans = 0;\r\n    for (const int num : nums) {\r\n      cur_sum += num;      \r\n      ans += counts[cur_sum - k];\r\n      ++counts[cur_sum];\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u95ee\u6709\u591a\u5c11\u5b50\u6570\u7ec4\u7684\u548c\u4e3ak\u3002 https:\/\/leetcode.com\/problems\/subarray-sum-equals-k\/description\/ Given an array of integers and an integer\u00a0k, you need to find the total number of continuous subarrays whose sum equals to\u00a0k.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,200],"class_list":["post-2297","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2297","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=2297"}],"version-history":[{"count":9,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2297\/revisions"}],"predecessor-version":[{"id":2307,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2297\/revisions\/2307"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}