-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxCounters.js
More file actions
32 lines (30 loc) · 859 Bytes
/
MaxCounters.js
File metadata and controls
32 lines (30 loc) · 859 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
//Score: 100%
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, A) {
// write your code in JavaScript (Node.js 8.9.4)
var counters = new Array(N).fill(0);
var max = 0;
var floor = 0;
var maxSum = 0;
var Alength = A.length;
for(var i = 0; i < Alength; i++){
var cur = A[i];
if(cur == N + 1){
floor = max;
}else{
if(counters[cur - 1] < floor){
counters[cur - 1] = floor;
}
counters[cur - 1] = counters[cur - 1] + 1;
if(counters[cur - 1] > max){
max = counters[cur - 1];
}
}
}
for(var j=0; j < N; j++){
if(counters[j] < floor)
counters[j] = floor;
}
return counters;
}