Skip to content

Commit 19cab52

Browse files
author
limhizy15
committed
[Codility] CyclicRotation 문제풀이
1 parent 034a748 commit 19cab52

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Array/Codility_CyclicRotation.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* array A, integer N
3+
* rotation -> 오른쪽으로 하나씩 밀어
4+
* goal: rotate array A K times
5+
*/
6+
7+
function solution(A, K) {
8+
const lastIndexOfA = A.length - 1
9+
10+
if (A.length < 1 || A % K === 0) {
11+
return []
12+
}
13+
14+
for (let i = 0; i < K; i++) {
15+
A = [A[lastIndexOfA]].concat(A.slice(0, lastIndexOfA))
16+
}
17+
18+
return A
19+
}
20+
21+
console.log(solution([3, 8, 9, 7, 6], 3))

0 commit comments

Comments
 (0)