forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
60 lines (47 loc) · 1.3 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// Source : https://leetcode.com/problems/subarrays-with-k-different-integers/
/// Author : liuyubobobo
/// Time : 2019-02-13
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cassert>
using namespace std;
/// Sliding Window
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int subarraysWithKDistinct(vector<int>& A, int K) {
unordered_map<int, int> set_left, set_right;
int left = 0, right = 0, res = 0;
for(int e: A){
set_left[e] ++;
set_right[e] ++;
while(set_left.size() > K){
set_left[A[left]] --;
if(set_left[A[left]] == 0)
set_left.erase(A[left]);
left ++;
}
while(set_right.size() >= K){
set_right[A[right]] --;
if(set_right[A[right]] == 0)
set_right.erase(A[right]);
right ++;
}
res += right - left;
}
return res;
}
};
int main() {
vector<int> A1 = {1,2,1,2,3};
int K1 = 2;
cout << Solution().subarraysWithKDistinct(A1, K1) << endl;
// 7
vector<int> A2 = {1,2,1,3,4};
int K2 = 3;
cout << Solution().subarraysWithKDistinct(A2, K2) << endl;
// 3
return 0;
}