-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmaxCounters.js
More file actions
34 lines (28 loc) · 744 Bytes
/
maxCounters.js
File metadata and controls
34 lines (28 loc) · 744 Bytes
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
function solution(N, A) {
const counters = new Array(N).fill(0);
const M = A.length;
let currentMax = 0;
let lastMaxCounter = 0;
for (let i = 0; i < M; i++) {
const current = A[i];
const currentCounter = counters[current - 1];
if (current <= N) {
if (currentCounter < lastMaxCounter) {
counters[current - 1] = lastMaxCounter + 1;
} else {
counters[current - 1] = currentCounter + 1;
}
if (counters[current - 1] > currentMax) {
currentMax = counters[current - 1];
}
} else {
lastMaxCounter = currentMax;
}
}
for (let i = 0; i < N; i++) {
if (counters[i] < lastMaxCounter) {
counters[i] = lastMaxCounter;
}
}
return counters;
}