[코딜리티] 레슨 2 - Cyclic Rotation (Swift)

devapploper·2024년 8월 11일
public func solution(_ A : inout [Int], _ K : Int) -> [Int] {
    let rotations: Int = K % A.count
    
    if K == 0 || A.isEmpty || rotations == 0 {
        return A
    }

    var firstPart: [Int] = [], secondPart: [Int] = []

    for i in 0..<A.count {
        if i < (A.count - rotations) {
            secondPart.append(A[i])
        } else {
            firstPart.append(A[i])
        }
    }

    return firstPart + secondPart
}
profile
iOS, 알고리즘, 컴퓨터공학에 관련 포스트를 정리해봅니다

0개의 댓글