ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [codility] Arrays - CyclicRotation 문제풀이
    Algorithm 2021. 9. 8. 09:14

    문제 원본

    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, 7] (elements are shifted right by one index and 6 is moved to the first place).

    The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

    Write a function:

    public func solution(_ A : inout [Int], _ K : Int) -> [Int]

    that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

    For example, given

    A = [3, 8, 9, 7, 6] K = 3

    the function should return [9, 7, 6, 3, 8]. Three rotations were made:

    [3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]

    For another example, given

    A = [0, 0, 0] K = 1

    the function should return [0, 0, 0]

    Given

    A = [1, 2, 3, 4] K = 4

    the function should return [1, 2, 3, 4]

    Assume that:

    • N and K are integers within the range [0..100];
    • each element of array A is an integer within the range [−1,000..1,000].

    In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

     

    문제 해설

    배열(A)과 이동할 값(K)이 주어짐

    배열이 오른쪽으로 한칸씩 이동

    배열의 마지막에 있는 값은 배열은 0번째 위치로 이동

    이동이 완료된 최종 배열을 제출

     

    구현 해설

    1. 배열의 길이와 이동할 거리로 순차적으로 최종 위치를 구함

    2. 다양한 케이스에 적합한 형태는 아님

    3. 제출 해보니 예상하지 못한 테스트 데이터에서 부적합 판정

    4. input text.txt 파일에 다양한 케이스를 입력하여 개발하는 방법 확인

    import Foundation
    import Glibc
    
    // you can write to stdout for debugging purposes, e.g.
    // print("this is a debug message")
    
    public func solution(_ A : inout [Int], _ K : Int) -> [Int] {
        // write your code in Swift 4.2.1 (Linux)
    
        var arr: [Int] = [Int](repeating: 0, count: A.count)
        if A.count == K {
            return A
        }
    
        let size = A.count
        for i in 0..<size {
            let cursor = size-i-K
            if cursor > 0 {
                arr[size-cursor] = A[i]
            } else if cursor < 0 {
                arr[-cursor] = A[i]
            } else {
                arr[0] = A[i]
            }
        }
        return arr
    }

     

     

     

    구현 해설

    1. 한칸씩 배열 이동

    import Foundation
    import Glibc
    
    // you can write to stdout for debugging purposes, e.g.
    // print("this is a debug message")
    
    public func solution(_ A : inout [Int], _ K : Int) -> [Int] {
        // write your code in Swift 4.2.1 (Linux)
        //array copy 
        var tempArr = A
    
        //길이와 이동해야할 수가 같으면 제자리 처리
        if A.count == K {
            return A
        }
        
        //하나씩 이동시작
        for _ in 0..<K {
            for i in 0..<A.count {
                if i+1 == A.count {
                    A[0] = tempArr[i]
                } else {
                    A[i+1] = tempArr[i]
                }
            }
            tempArr = A
        }
    
        return tempArr
    }

     

    댓글

Designed by Tistory.