# LeetCode 第992题 Subarrays with K Different Integers【滑动窗口】（Java）

1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length

## “LeetCode 第992题 Subarrays with K Different Integers【滑动窗口】（Java）”上的5条回复 吉米说： tinyfool说： 吉米说：

int atMostK(vector& A, int K)
{
int i = 0, res = 0;
unordered_map count;
for (int j = 0; j < A.size(); ++j)
{
if (!count[A[j]]++) K–;
while (K < 0)
{
if (!–count[A[i]]) K++;
i++;
}
res += j – i + 1;
}
return res;
}

subarraysWithMostKDistinct(k)
1
12
121
1213
134

subarraysWithMostKDistinct(k-1)
1
12
121
13
34 tinyfool说：

1
2
3
4
12
21
121
13
34
1213
213
134

1
2
3
4
12
21
121
13
34 吉米说：

2
3
4
21
13
34
213
{"ft_lang_label":"__label__zh","ft_lang_prob":0.7938704,"math_prob":0.9917575,"size":2725,"snap":"2020-34-2020-40","text_gpt3_token_len":1908,"char_repetition_ratio":0.2061742,"word_repetition_ratio":0.06842105,"special_character_ratio":0.29027522,"punctuation_ratio":0.18984547,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.952955,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-27T22:16:04Z\",\"WARC-Record-ID\":\"<urn:uuid:3ae70eed-2ac5-49ce-a346-8bc7beaeebf6>\",\"Content-Length\":\"67355\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bca85332-0cc7-407c-8b83-8edee91d7774>\",\"WARC-Concurrent-To\":\"<urn:uuid:df055038-5831-453c-9216-2befd1d9ef87>\",\"WARC-IP-Address\":\"149.129.81.11\",\"WARC-Target-URI\":\"https://codechina.org/2019/05/leetcode-992-subarrays-with-k-different-integers-java/\",\"WARC-Payload-Digest\":\"sha1:TQUWJ5YOBPDA6ILIAT4QHE6RG4BZT7ZE\",\"WARC-Block-Digest\":\"sha1:XB74YKWPTHV7RQKLTYT4FIPRCTP6AJKN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600401582033.88_warc_CC-MAIN-20200927215009-20200928005009-00588.warc.gz\"}"}