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
function solution(X, A) {
let numbers = new Set();

for (let i = 0, n = A.length; i < n; i++) {
numbers.add(A[i]);

if (X === numbers.size) {
return i;
}
}

return -1;
}


'IT General' 카테고리의 다른 글

배열의 대각선 출력  (2) 2018.09.26
Codility #11-MaxCounters  (0) 2018.09.18
Codility #9-PermCheck  (0) 2018.09.18
Codility #8-TapeEquilibrium  (0) 2018.09.18
Codility #7-Brackets  (0) 2018.08.18
function solution(A) {
let numbers = {};
let n = A.length;
let sumAll = 0;
let sumN = n * (n + 1) / 2; // sum(1~N):

for (let i = 0; i < n; i++) {
if (numbers[A[i]]) {
return 0; // duplicated
} else {
numbers[A[i]] = true;
}
sumAll += A[i];
}

return sumAll === sumN ? 1 : 0;
}


'IT General' 카테고리의 다른 글

Codility #11-MaxCounters  (0) 2018.09.18
Codility #10-FrogRiverOne  (0) 2018.09.18
Codility #8-TapeEquilibrium  (0) 2018.09.18
Codility #7-Brackets  (0) 2018.08.18
Codility #7-Nesting  (0) 2018.08.18

+ Recent posts