ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [codility] Counting Elements - MaxCounters
    Algorithm 2021. 9. 23. 23:41

    문제

    You are given N counters, initially set to 0, and you have two possible operations on them:

    • increase(X) − counter X is increased by 1,
    • max counter − all counters are set to the maximum value of any counter.

    A non-empty array A of M integers is given. This array represents consecutive operations:

    • if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
    • if A[K] = N + 1 then operation K is max counter.

    For example, given integer N = 5 and array A such that:

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

    the values of the counters after each consecutive operation will be:

    (0, 0, 1, 0, 0) (0, 0, 1, 1, 0) (0, 0, 1, 2, 0) (2, 2, 2, 2, 2) (3, 2, 2, 2, 2) (3, 2, 2, 3, 2) (3, 2, 2, 4, 2)

    The goal is to calculate the value of every counter after all operations.

    Write a function:

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

    that, given an integer N and a non-empty array A consisting of M integers, returns a sequence of integers representing the values of the counters.

    Result array should be returned as an array of integers.

    For example, given:

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

    the function should return [3, 2, 2, 4, 2], as explained above.

    Write an efficient algorithm for the following assumptions:

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

      Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

     

    문제 해설

    A[K] = X, 1 <= X <= N 카운트 증가

    A[K] = N + 1 이면 최대 카운트값 전체적용

     

    구현 해설

    X값이 1보다 크거나 같고 N보다 작거나 같으면 해당 인덱스 카운팅 값 증가

    큰값이나 예측하지 못한 데이터가 들어오는 경우가 발생

    arr 에 현재 카운팅중 가장 큰값을 초기화하는 [Int](repeating: arr.max()!, count: N) 구문인듯 하다.

    다른 방법으로 계속 풀어봤으나 100%는 힘들어서 검색을 해봄

     

    https://app.codility.com/demo/results/trainingC62947-485/

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

     

    해결 힌트는 return 시 maxCounter와 arr에 카운트값과 비교하여 작은 값에 초기화

     

    https://app.codility.com/demo/results/trainingWYS26A-946/

    import Foundation
    import Glibc
    
    // you can write to stdout for debugging purposes, e.g.
    // print("this is a debug message")
    
    public func solution(_ N : Int, _ A : inout [Int]) -> [Int] {
        // write your code in Swift 4.2.1 (Linux)
        var arr = [Int](repeating: 0, count: N)
            
        var max = 0
        var maxCounter = 0
        for (index, _) in A.enumerated() {
            if A[index] > N {
                maxCounter = max
            } else {
                if arr[A[index]-1] < maxCounter {
                    arr[A[index]-1] = maxCounter
                }
                    
                arr[A[index]-1] += 1
                    
                if max < arr[A[index]-1] {
                    max = arr[A[index]-1]
                }
            }
        }
        return arr.map({ $0 < maxCounter ? maxCounter : $0 })
    }

    댓글

Designed by Tistory.