Skip to content

Commit c352fbc

Browse files
committed
codility problems: practice
1 parent 9db8902 commit c352fbc

14 files changed

+988
-0
lines changed

codility/abLetters.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function solution(S) {
2+
let strArr = S.split("");
3+
let first = 0;
4+
let second = 1;
5+
6+
while (second > first && second < S.length) {
7+
if (strArr[first] === "b") {
8+
if (strArr[second] === "a") {
9+
return false;
10+
} else {
11+
first = second;
12+
second++;
13+
}
14+
} else {
15+
first = second;
16+
second++;
17+
}
18+
}
19+
20+
return true;
21+
}
22+
23+
console.log(solution("aabbabb"));
24+
25+
/**
26+
* aabbb => true 11000
27+
* ba => false 01
28+
* aaa => true 111
29+
* b => true 0
30+
* abba => false 1001
31+
*/

codility/binaryGap.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Binary Gap
3+
*
4+
* 1 0 0 0 1 0 0 1 0 1 => 3
5+
*
6+
*
7+
*/
8+
9+
function solution(N) {
10+
const bin = N.toString(2).split("");
11+
let max = 0; // the initial maximum gap
12+
let currMax = 0; // the current maximum gap
13+
let left = 0;
14+
let right = 1;
15+
16+
while (left < right && right < bin.length) {
17+
if (bin[left] === "0") {
18+
left++;
19+
right++;
20+
currMax = 0;
21+
} else {
22+
if (bin[right] === "0") {
23+
currMax++;
24+
right++;
25+
} else {
26+
max = Math.max(currMax, max);
27+
currMax = 0;
28+
left = right;
29+
right++;
30+
}
31+
}
32+
}
33+
34+
return max;
35+
}
36+
37+
console.log(solution(32));

codility/binaryTree.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function solution() {
2+
// write your code in JavaScript (Node.js 8.9.4)
3+
const longestUnivaluePath = (root) => {
4+
const helper = (node) => {
5+
if (!node) {
6+
return 0;
7+
}
8+
9+
let left = helper(node.left);
10+
let right = helper(node.right);
11+
12+
if (node.left) {
13+
left += node.left.val === node.val ? 1 : -left;
14+
}
15+
16+
if (node.right) {
17+
right += node.right.val === node.val ? 1 : -right;
18+
}
19+
20+
max = Math.max(max, left + right);
21+
22+
return Math.max(left, right);
23+
};
24+
25+
let max = 0;
26+
helper(root);
27+
return max;
28+
};
29+
30+
return longestUnivaluePath(T);
31+
}

codility/frogJump.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Frog Jumping
3+
*/
4+
5+
function solution(X, Y, D) {
6+
return Math.ceil((Y - X) / D);
7+
}
8+
9+
console.log(solution(1, 5, 3));

codility/frogJump2.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* frog jump
3+
*/
4+
5+
function solution(X, A) {
6+
let set = new Set();
7+
for (let i = 1; i <= X; i++) {
8+
set.add(i);
9+
}
10+
11+
for (let i = 0; i < A.length; i++) {
12+
if (set.has(A[i])) {
13+
set.delete(A[i]);
14+
}
15+
if (set.size === 0) {
16+
return i;
17+
}
18+
}
19+
20+
return -1;
21+
}
22+
23+
console.log(solution(2, [2, 2, 2, 2, 2]));

codility/maxCounter.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Max Counter
3+
*/
4+
5+
function solution(N, A) {
6+
let counter = new Array(N).fill(0);
7+
let max = 0;
8+
9+
for (let i = 0; i < A.length; i++) {
10+
if (A[i] <= N) {
11+
counter[A[i] - 1]++;
12+
max = Math.max(max, counter[A[i] - 1]);
13+
} else {
14+
counter = new Array(N).fill(max);
15+
}
16+
}
17+
18+
return counter;
19+
}
20+
21+
console.log(solution(5, [1, 2, 1, 4, 6, 3]));
22+
23+
/**
24+
* 0 0 0 0 0
25+
* 1 0 0 0 0
26+
* 1 1 0 0 0
27+
* 2 1 0 0 0
28+
* 2 1 0 1 0
29+
* 2 2 2 2 2
30+
* 2 2 3 2 2
31+
*/

codility/minLargestMissing.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Minimum largest missing integer
3+
*/
4+
5+
function solution(A) {
6+
let currMax = 0;
7+
let currMin = Number.MAX_VALUE;
8+
for (let i = 0; i < A.length; i++) {
9+
currMax = Math.max(currMax, A[i]);
10+
currMin = Math.min(currMin, A[i]);
11+
}
12+
13+
if (currMax <= 0) return 1;
14+
15+
let max = currMax;
16+
while (currMax >= currMin) {
17+
currMax--;
18+
if (!A.includes(currMax)) {
19+
return currMax;
20+
} else {
21+
continue;
22+
}
23+
}
24+
25+
return currMin > 1 ? 1 : max + 1;
26+
}
27+
28+
console.log(solution([90, 91, 92, 93]));

codility/missingInteger.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Missing integer
3+
*/
4+
5+
function solution(A) {
6+
let set = new Set();
7+
for (let i = 0; i < A.length; i++) {
8+
set.add(A[i]);
9+
}
10+
for (let i = 1; i <= A.length + 1; i++) {
11+
if (!set.has(i)) {
12+
return i;
13+
}
14+
}
15+
}
16+
17+
console.log(solution([2, 3, 1, 5]));

codility/oddElement.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Odd element of array
3+
*/
4+
5+
function solution(A) {
6+
if (A.length === 1) return A[0];
7+
let frequency = {};
8+
9+
for (let i = 0; i < A.length; i++) {
10+
frequency[A[i]] = frequency[A[i]] + 1 || 1;
11+
}
12+
13+
for (el in frequency) {
14+
if (frequency[el] % 2 !== 0) {
15+
return +el;
16+
}
17+
}
18+
}
19+
20+
console.log(solution([9, 3, 9, 3, 9, 7, 9]));

0 commit comments

Comments
 (0)