function solution(S) {
let stack = [];

for (let i = 0, n = S.length; i < n; i++) {
let c = S.charAt(i);

switch (c) {
case '{':
case '[':
case '(':
stack.push(c);
break;
case '}':
if (stack.pop() !== '{') return 0;
break;
case ']':
if (stack.pop() !== '[') return 0;
break;
case ')':
if (stack.pop() !== '(') return 0;
break;
}
}

return stack.length === 0 ? 1 : 0;
}


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

Codility #9-PermCheck  (0) 2018.09.18
Codility #8-TapeEquilibrium  (0) 2018.09.18
Codility #7-Nesting  (0) 2018.08.18
Codility #3-PermMissingElem  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18
function solution(S) {
let stack = [];

for (let i = 0, n = S.length; i < n; i++) {
let c = S.charAt(i);
if (c === '(') {
stack.push(c);
} else if (c === ')') {
if (stack.length === 0) {
return 0;
} else if (stack[stack.length - 1] === '(') {
stack.pop();
}
}
}

return stack.length === 0 ? 1 : 0;
}


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

Codility #8-TapeEquilibrium  (0) 2018.09.18
Codility #7-Brackets  (0) 2018.08.18
Codility #3-PermMissingElem  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18
Codility #2-OddOccurrencesInArray  (0) 2018.08.18
function solution(A) {
let n = A.length + 1;
let sumAll = n * (n + 1) / 2; // sum(1..N)
let sumArray = A.reduce((prev, curr) => prev + curr, 0);

return sumAll - sumArray;
}


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

Codility #7-Brackets  (0) 2018.08.18
Codility #7-Nesting  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18
Codility #2-OddOccurrencesInArray  (0) 2018.08.18
Codility #2-CyclicRotation  (0) 2018.08.18
function solution(X, Y, D) {
return Math.ceil((Y - X) / D);
}


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

Codility #7-Nesting  (0) 2018.08.18
Codility #3-PermMissingElem  (0) 2018.08.18
Codility #2-OddOccurrencesInArray  (0) 2018.08.18
Codility #2-CyclicRotation  (0) 2018.08.18
Fibonacci function without recursion  (0) 2018.08.15
function solution(A) {
let map = {};

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

if (!map[key]) {
map[key] = 0;
}

map[key]++;
}

for (let key in map) {
if (map[key] % 2 === 1) {
return parseInt(key);
}
}
}


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

Codility #3-PermMissingElem  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18
Codility #2-CyclicRotation  (0) 2018.08.18
Fibonacci function without recursion  (0) 2018.08.15
Codility #1-BinaryGap  (0) 2018.08.15
function solution(A, K) {
let arr = Array.from(A); // clone
let n = K % A.length; // for better performance

for (let i = 0; i < n; i++) {
let val = arr.pop();
arr.unshift(val);
}

return arr;
}

function solution(A, K) {
let arr = [];

for (let i = 0, j; i < A.length; i++) {
j = (i + K) % A.length;
arr[j] = A[i];
}

return arr;
}


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

Codility #3-PermMissingElem  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18
Codility #2-OddOccurrencesInArray  (0) 2018.08.18
Fibonacci function without recursion  (0) 2018.08.15
Codility #1-BinaryGap  (0) 2018.08.15
function fibonacci(N) {
if (N <= 2) {
return 1;
}

let a = 1; // N - 2
let b = 1; // N - 1
let result;

for (let i = 3; i <= N; i++) {
result = a + b;
a = b;
b = result;
}

return result;
}


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

Codility #3-PermMissingElem  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18
Codility #2-OddOccurrencesInArray  (0) 2018.08.18
Codility #2-CyclicRotation  (0) 2018.08.18
Codility #1-BinaryGap  (0) 2018.08.15
function solution(N) {
let trimmed = (N).toString(2).replace(/0+$/, ''); // Binary string right trimmed with '0' characters.
let gaps = trimmed.split('1').map((item) => item.length);

return Math.max(...gaps);
}


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

Codility #3-PermMissingElem  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18
Codility #2-OddOccurrencesInArray  (0) 2018.08.18
Codility #2-CyclicRotation  (0) 2018.08.18
Fibonacci function without recursion  (0) 2018.08.15

+ Recent posts