โŒจ๏ธ Language/swift

[Swift] Codility Lesson 4 - MaxCounters

hyebin (Helia) 2022. 7. 23. 19:08
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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 anefficientalgorithm 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].

 

๋ฌธ์ œ ๋งํฌ
 

MaxCounters coding task - Learn to Code - Codility

Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.

app.codility.com

GitHub
 

GitHub - yoohyebin/swift

Contribute to yoohyebin/swift development by creating an account on GitHub.

github.com

 

๋ฌธ์ œ ๋ถ„์„

  • ์ •์ˆ˜ N๊ณผ ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด A๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.
  • 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ N๊ฐœ์˜ ์š”์†Œ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” counter๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
    • ์ฆ๊ฐ€: A[k] = X, 1 ≤ X ≤ N, counter[X]๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
    • max counter: A[K] = N + 1, ๋ชจ๋“  counter์˜ ๊ฐ’์„ counter ์š”์†Œ๋“ค ์ค‘ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๋ฐ”๊พผ๋‹ค.

 

ํ•ด๊ฒฐ๋ฐฉ์•ˆ 1 - ์‹คํŒจ

  1. N๊ฐœ์˜ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ re ๋ฐฐ์—ด ์ƒ์„ฑ
  2. ์ž…๋ ฅ๋œ ๋ฐฐ์—ด A์˜ ์š”์†Œ ๊ฐ’์ด N+1์ด๋ผ๋ฉด ๋ฐฐ์—ด re์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์„ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ
  3. ์ž…๋ ฅ๋œ ๋ฐฐ์—ด A์˜ ์š”์†Œ ๊ฐ’์ด N+1์ด ์•„๋‹ˆ๋ผ๋ฉด re[์š”์†Œ ๊ฐ’]์„ 1 ์ฆ๊ฐ€

Solution 1

public func solution(_ N : Int, _ A : inout [Int]) -> [Int] {
    var re: [Int] = Array(repeating: 0, count: N)

    for i in A{
        if i == N+1{
            re = Array(repeating: re.max()!, count: N)
        }else{
            re[i-1] += 1
        }
    }
    return re
}

  • ์ž…๋ ฅ์ด ํฐ ๊ฒฝ์šฐ timeout ์˜ค๋ฅ˜ ๋ฐœ์ƒ
  • max( )์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N)
  • Array(repeating:, count:)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(๋ฐฐ์—ด ๊ธธ์ด)

 

ํ•ด๊ฒฐ๋ฐฉ์•ˆ 2 - ์„ฑ๊ณต

  1. N๊ฐœ์˜ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ counter ๋ฐฐ์—ด ์ƒ์„ฑ
  2. counter๋ฐฐ์—ด์˜ ์ตœ๋Œ“๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ maxValue์™€ max counter ๋™์ž‘์„ ํ›„์ฒ˜๋ฆฌ ํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ maxCounter ์„ ์–ธ
  3. ๋ฐฐ์—ด A์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ํ™•์ธ <๋ฐฐ์—ด A์˜ ์š”์†Œ  = i>
    • i๊ฐ€ N+1์ด๋ผ๋ฉด, maxCounter ๋ณ€์ˆ˜์— ํ˜„์žฌ counter ๋ฐฐ์—ด์˜ ์ตœ๋Œ“๊ฐ’์ด ๋‹ด๊ฒจ์žˆ๋Š” maxValue ๊ฐ’์„ ๋„ฃ์Œ
    • i๊ฐ€ N+1์ด ์•„๋‹ˆ๋ผ๋ฉด
      1. counter[i-1]์ด maxCounter๋ณด๋‹ค ์ž‘๋‹ค๋ฉด counter[i-1]์„ maxCounter๋กœ ๋ณ€๊ฒฝ
      2. counter[i-1]์„ 1 ์ฆ๊ฐ€
      3. ์ตœ๋Œ“๊ฐ’์„ ๋‹ด์€ maxValue ๋ณ€์ˆ˜์™€ ๋น„๊ตํ•˜์—ฌ ์ตœ๋Œ“๊ฐ’ update
  4. counter ๋ฐฐ์—ด์—์„œ maxCounter ๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ด ์žˆ๋‚˜ ์ˆœ์„œ๋Œ€๋กœ ํƒ์ƒ‰, ์ž‘์€ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด maxCounter ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ

Solution 2

public func solution(_ N : Int, _ A : inout [Int]) -> [Int] {
    var counter: [Int] = Array(repeating: 0, count: N)
    var maxValue = 0, maxCounter = 0

    for i in A{
        if i == N+1{
            maxCounter = maxValue
        }else{
            if counter[i-1] < maxCounter{
                counter[i-1] = maxCounter
            }
            counter[i-1] += 1
            maxValue = max(counter[i-1], maxValue)
        }
    }
    for index in 0..<counter.count{
        if counter[index] < maxCounter {counter[index] = maxCounter}
    }

    return counter
}

  • ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N + M)
๋ฐ˜์‘ํ˜•