-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaxCounters.cpp
33 lines (31 loc) · 944 Bytes
/
MaxCounters.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
// Accessible from https://app.codility.com/programmers/lessons/4-counting_elements/max_counters/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected Time Complexity: O(N + M)
//
vector<int> solution(int N, vector<int> &A) {
vector<int> out(N);
int maxCount = 0;
int maxIndex = -1;
int maxValue = -1;
unsigned int n = A.size();
for (unsigned int i = 0; i < n; ++i) {
if (A[i] <= N && A[i] >= 1) {
if (out[A[i]-1] < maxValue) out[A[i]-1] = maxValue;
out[A[i]-1]++;
if (out[A[i]-1] > maxCount) maxCount = out[A[i]-1];
} else if (A[i] == N + 1) {
maxIndex = i;
maxValue = maxCount;
}
}
if (maxIndex != -1) {
out = vector<int>(N, maxValue);
for (unsigned int i = maxIndex + 1; i < n; ++i)
if (A[i] <= N && A[i] >= 1)
out[A[i]-1]++;
}
return out;
}