Skip to content

[Algorithm] CyclicRotationย #129

@hwangJi-dev

Description

@hwangJi-dev

๐Ÿ’ฌย ๋ฌธ์ œ

https://app.codility.com/programmers/lessons/2-arrays/cyclic_rotation/


๐Ÿ’ฌย Idea

  • K๋ฒˆ์„ ๋Œ๋ฉด์„œ ๋ฐฐ์—ด A์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ์ถ”์ถœํ•˜๊ณ , ์ฒซ๋ฒˆ์งธ์— ๊ทธ ์›์†Œ๋ฅผ ์ง‘์–ด๋„ฃ๋Š”๊ฒƒ์„ ๋ฐ˜๋ณตํ•˜์ž
    • ์ฃผ์˜! ๋นˆ ๋ฐฐ์—ด ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜ ์ฃผ์˜

๐Ÿ’ฌย ํ’€์ด

import Foundation

public func solution(A: inout [Int], K: Int) -> [Int] {
    if A.isEmpty { return [] }
    
    for _ in 0..<K {
        A.insert(A.removeLast(), at: 0)
    }
    
    return A
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions