-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxCounters.js
More file actions
37 lines (28 loc) · 918 Bytes
/
MaxCounters.js
File metadata and controls
37 lines (28 loc) · 918 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
35
36
37
function solution(N, A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxCounter = 0;
let tempMaxCounter = 0;
let counts = new Array(N);
for(let i = 0; i < A.length; i++){
let counterIndex = A[i] - 1;
if(counterIndex >= N){
maxCounter = tempMaxCounter;
continue;
}
if(counts[counterIndex] == null){
counts[counterIndex] = 0;
}
if(counts[counterIndex] < maxCounter)
{
counts[counterIndex] = maxCounter + 1;
}else{
counts[counterIndex] = counts[counterIndex] + 1;
}
tempMaxCounter = counts[counterIndex] > tempMaxCounter ? counts[counterIndex] : tempMaxCounter;
}
for(let i = 0; i < N; i++){
if(counts[i] < maxCounter || counts[i] == null)
counts[i] = maxCounter;
}
return counts;
}