Simple&Natural

Codility lesson2 - cyclic rotation 본문

코딩테스트 풀이/Codility

Codility lesson2 - cyclic rotation

Essense 2022. 4. 18. 00:37
728x90

사이클 횟수 K가 배열 크기의 배수가 되는 case만 조심하면 된다.

 

fun solution(A: IntArray, K: Int): IntArray {
    val answer = IntArray(A.size)
    val fixedK = 
    A.forEachIndexed { index, value ->
        if ((index + K%A.size) < A.size) {
            answer[index + K%A.size] = value
        } else {
            answer[index + K%A.size - A.size] = value
        }
    }
    return answer
}

 

https://app.codility.com/demo/results/training8CAMW9-568/

 

Test results - Codility

An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9,

app.codility.com

 

728x90

'코딩테스트 풀이 > Codility' 카테고리의 다른 글

Codility lesson3 - TapeEquilibrium  (0) 2022.04.28
Codility lesson3 - PermMissingElem  (0) 2022.04.27
Codility lesson3 - FrogJmp  (0) 2022.04.20
Codility lesson2 - odd  (0) 2022.04.20
Codilidy lesson1 - binary gap  (0) 2022.04.17