-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountDistinctSlices
48 lines (42 loc) · 1 KB
/
CountDistinctSlices
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
// 100% solution
int solution(int M, vector<int> &A) {
int n = A.size();
long result = 0;
int front = 0;
int back = 0;
vector<bool> seen(M, false);
while (front < n) {
if (back < n && !seen[A[back]]) {
seen[A[back]] = true;
++back;
}
else {
result += (back - front);
if (result > 1000000000) {
return 1000000000;
}
seen[A[front]] = false;
front++;
}
}
return result;
}
// 70% solution
#include <set>
int solution(int M, vector<int> &A) {
int result = 0;
set<int> cat;
for (size_t i = 0; i < A.size(); ++i) {
for (size_t j = i; j < A.size(); ++j) {
if (cat.insert(A[j]).second) {
if (++result >= 1000000000) {
return 1000000000;
};
continue;
}
break;
}
cat.clear();
}
return result;
}