Skip to content

Commit 9b5433d

Browse files
committed
solve : codility 4 problems
- binary gap - cyclic rotation - odd occurrences in array - stone wall
1 parent 0fee391 commit 9b5433d

5 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const solution = (N) => {
2+
let maxLength = 0;
3+
const bin = N.toString(2).split('');
4+
5+
bin.reduce((length, word) => {
6+
if (word === '1') {
7+
if (maxLength < length) maxLength = length;
8+
return 0;
9+
}
10+
return length + 1;
11+
}, 0);
12+
13+
return maxLength;
14+
};
15+
16+
console.log(solution(32));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const solution = (A, K) => {
2+
const length = A.length;
3+
const k = K % length;
4+
const front = A.slice(length - k, length);
5+
const end = A.slice(0, length - k);
6+
return front.concat(end);
7+
};
8+
9+
console.log(solution([3, 8, 9, 7, 6], 3));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const solution = (A) => {
2+
const count = A.reduce((arr, num) => {
3+
if (arr[num] === undefined) arr[num] = 1;
4+
else arr[num] += 1;
5+
return arr;
6+
}, {});
7+
for (const num in count) {
8+
if (count[num] % 2 !== 0) return num;
9+
}
10+
return -1;
11+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const solution = (H) => {
2+
const queue = [H];
3+
let block = 0;
4+
while (queue.length > 0) {
5+
const h = queue.pop();
6+
if (h.length === 0) continue;
7+
const min = Math.min.apply(null, h);
8+
const remain = h.reduce((arr, cur) => {
9+
if (cur === min) {
10+
queue.push(arr);
11+
return [];
12+
}
13+
arr.push(cur);
14+
return arr;
15+
}, []);
16+
queue.push(remain);
17+
block += 1;
18+
}
19+
return block;
20+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 📋 Codility 점수 기록
2+
3+
| Lesson | 문제 | 점수 | 비고 |
4+
| :-----------------: | :-------------------: | :--: | :-------------------: |
5+
| 1-Iterations | BinaryGap | 100 | - |
6+
| 2-Arrays | CyclicRotation | 100 | - |
7+
| 2-Arrays | OddOccurrencesInArray | 100 | - |
8+
| 7-Stacks and Queues | StoneWall | 85 | 정확도 100%, 성능 77% |

0 commit comments

Comments
 (0)