function solution(N, A) {
let prevMax = 0;
let maxCounter = 0;
let counters = Array(N).fill(0);
for (let i = 0, len = A.length; i < len; i++) {
if (A[i] <= N) {
let idx = A[i] - 1;
if (counters[idx] < prevMax) {
counters[idx] = prevMax + 1;
} else {
counters[idx]++;
}
if (maxCounter < counters[idx]) {
maxCounter = counters[idx];
}
} else {
prevMax = maxCounter;
}
}
for (let i = 0; i < N; i++) {
if (counters[i] < prevMax) {
counters[i] = prevMax;
}
}
return counters;
}
'IT General' 카테고리의 다른 글
배열의 대각선 출력 #2 (2) | 2018.09.27 |
---|---|
배열의 대각선 출력 (2) | 2018.09.26 |
Codility #10-FrogRiverOne (0) | 2018.09.18 |
Codility #9-PermCheck (0) | 2018.09.18 |
Codility #8-TapeEquilibrium (0) | 2018.09.18 |