ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [codility] Arrays - OddOccurrencesInArray 문제풀이
    Algorithm 2021. 9. 22. 21:44

    문제 원본

    O(N) or O(N*log(N))

    A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

    For example, in array A such that:

    A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9

    • the elements at indexes 0 and 2 have value 9,
    • the elements at indexes 1 and 3 have value 3,
    • the elements at indexes 4 and 6 have value 9,
    • the element at index 5 has value 7 and is unpaired.

    Write a function:

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

    that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.

    For example, given array A such that:

    A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9

    the function should return 7, as explained in the example above.

    Write an efficient algorithm for the following assumptions:

    • N is an odd integer within the range [1..1,000,000];
    • each element of array A is an integer within the range [1..1,000,000,000];
    • all but one of the values in A occur an even number of times.Copyright 2009–2021 by Codility Limited. All 

     

    Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

     

     

    문제 해설

    N개의 정수가 담긴 비어있지 않은 배열 A가 제공

    값이 동일하면 짝을 이룰수 있고, 동일한 값이 없는 값을 리턴

    정리하면 배열 [1,2,3,4,5,2,3,4,1] 이 주어질때 짝이 없는 5를 검출

    구현 해설

    - 주어진 배열 정렬

    case1. [2,5,3,4,5,2,1,3,5] -> [1,1,2,2,3,3,4,5,5] 

    case1. [2,5,3,4,4,2,1,3,1] -> [1,1,2,2,3,3,4,4,5

    case2. [2,1,4,3,5,5,4,2,3] -> [1,2,2,3,3,4,4,5,5]

    case3. [1, 3, 2, 2, 5, 5, 3] -> [1,2,2,3,3,5,5]

    case4. [1, 1, 2, 2, 5, 5, 3] -> [1,1,2,2,3,5,5]

    case5. [1, 1, 2, 2, 3, 5, 3] -> [1,1,2,2,3,3,5]

    2칸씩 이동하면서 값 비교하여 짝이없는 값 찾기 

     

    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]) -> Int {
        // write your code in Swift 4.2.1 (Linux)
        var resultValue = 0
        let list = A.sorted()
        for index in stride(from: 0, to: list.count - 1, by: 2) {
            if list[index] != list[index + 1] {
                resultValue = list[index]
                break
            }   
            if index == list.count - 3 {
                resultValue = list[list.count - 1]
            }
        }
        return resultValue
    }

     

    결과

    88% 예외처리 부족

     

    예외 처리 추가

    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]) -> Int {
        // write your code in Swift 4.2.1 (Linux)
        if A.count == 1 {
            return A[0]
        }
        var resultValue = 0
        let list = A.sorted()
        for index in stride(from: 0, to: list.count - 1, by: 2) {
            if list[index] != list[index + 1] {
                resultValue = list[index]
                break
            }   
            if index == list.count - 3 {
                resultValue = list[list.count - 1]
            }
        }
        return resultValue
    }

    댓글

Designed by Tistory.