{"id":4835,"date":"2019-02-10T10:40:11","date_gmt":"2019-02-10T18:40:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4835"},"modified":"2019-02-10T10:42:53","modified_gmt":"2019-02-10T18:42:53","slug":"leetcode-992-subarrays-with-k-different-integers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-992-subarrays-with-k-different-integers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 992. Subarrays with K Different Integers"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>A<\/code>&nbsp;of positive integers, call a (contiguous, not necessarily distinct) subarray of&nbsp;<code>A<\/code>&nbsp;<em>good<\/em>&nbsp;if the number of different integers in that subarray is exactly&nbsp;<code>K<\/code>.<\/p>\n\n\n\n<p>(For example,&nbsp;<code>[1,2,3,1,2]<\/code>&nbsp;has&nbsp;<code>3<\/code>&nbsp;different integers:&nbsp;<code>1<\/code>,&nbsp;<code>2<\/code>, and&nbsp;<code>3<\/code>.)<\/p>\n\n\n\n<p>Return the number of good subarrays of&nbsp;<code>A<\/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>A = [1,2,1,2,3], K = 2\n<strong>Output: <\/strong>7\n<strong>Explanation: <\/strong>Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,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>A = [1,2,1,3,4], K = 3\n<strong>Output: <\/strong>3\n<strong>Explanation: <\/strong>Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 20000<\/code><\/li><li><code>1 &lt;= A[i] &lt;= A.length<\/code><\/li><li><code>1 &lt;= K &lt;= A.length<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: Two pointers + indirection<\/h2>\n\n\n\n<p>Let f(x) denote the number of subarrays with x or less distinct numbers.<br>ans = f(K) &#8211; f(K-1)<br>It takes O(n) Time and O(n) Space to compute f(x)<\/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\n\/\/ vector: 56 ms, 10.1 MB\n\/\/ Hashtable: 126 ms, 25 MB\nclass Solution {\npublic:\n  int subarraysWithKDistinct(vector<int>& A, int K) {\n    \/\/ Returns the number of subarrays with k or less distinct numbers.\n    auto subarrys = [&A](int k) {\n      vector<int> count(A.size() + 1);\n      int ans = 0;\n      int i = 0;\n      for (int j = 0; j < A.size(); ++j) {\n        if (count[A[j]]++ == 0) \n          --k;\n        while (k < 0)\n          if (--count[A[i++]] == 0) ++k;\n        ans += j - i + 1;\n      }\n      return ans;\n    };\n    return subarrys(K) - subarrys(K - 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-793-preimage-size-of-factorial-zeroes-function\/\">\u82b1\u82b1\u9171 LeetCode 793. Preimage Size of Factorial Zeroes Function<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;A&nbsp;of positive integers, call a (contiguous, not necessarily distinct) subarray of&nbsp;A&nbsp;good&nbsp;if the number of different integers in that subarray is exactly&nbsp;K. (For example,&nbsp;[1,2,3,1,2]&nbsp;has&nbsp;3&nbsp;different&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[217,82,215,175],"class_list":["post-4835","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-hard","tag-hashtable","tag-sliding-window","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4835","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=4835"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4835\/revisions"}],"predecessor-version":[{"id":4839,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4835\/revisions\/4839"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}