Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm

[Codility] (Swift) MaxCounters

๊ฐ์ž ๐Ÿฅ” 2022. 12. 22. 12:51
๋ฐ˜์‘ํ˜•

๋„ค์ด๋ฒ„ ์ฝ”ํ…Œ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด์„œ Codility๋ฅผ ์ฒ˜์Œ์œผ๋กœ ์ด์šฉํ•ด๋ณด์•˜๋‹ค. codility์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋ถ„์€ programmers์™€ ํฌ๊ฒŒ ๋‹ค๋ฅผ๋ฐ”๊ฐ€ ์—†์—ˆ๊ณ , ๋” ์ข‹์•˜๋˜ ์ ์€, ์‹œ๊ฐ„๋ณต์žก๋„์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ๋” ์ž์„ธํ•˜๊ฒŒ ํ•ด์ค€๋‹ค๋Š” ๊ฒƒ์ด์—ˆ๋‹ค!

 

์ผ๋‹จ '๊ธฐ์ดˆ'๋ถ€ํ„ฐ ๋‹ค์‹œ ์žก์ž๋Š” ๋งˆ์ธ๋“œ๋กœ ๊ณต๋ถ€์ค‘์ธ ์š”์ฆ˜, ์ผ๋‹จ Lesson์— ์žˆ๋Š” Medium ๋ฌธ์ œ๋“ค๋งŒ ๋จผ์ € ๊ณจ๋ผ์„œ ํ’€์—ˆ๋‹ค.

 

๐ŸŸ  ๋ฌธ์ œ 

https://app.codility.com/programmers/lessons/4-counting_elements/max_counters/start/

 

Codility

Your browser is not supported Please, update your browser or switch to a different one. Learn more about what browsers are supported

app.codility.com

๋ฌธ์ œ๋Š” ์ „๋ถ€... ์˜์–ด๋‹ค...๐Ÿฅน

 

๐ŸŸ  ์˜ค๋‹ต์œผ๋กœ ์•Œ์•„๊ฐ€๋Š” ๋‚ด ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ 

์ฒ˜์Œ ์˜ค๋‹ต ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

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

    for idx in 0..<A.count {
        if A[idx] == N+1 {
            let max = answer.max()!
            answer = Array(repeating: max, count: N)
        } else {
            answer[A[idx]-1] += 1
        }
        print(answer)
    }

    return answer
}


print(solution(N, &A))

์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ, ์ •๋‹ต์€ ๋งž์ท„์ง€๋งŒ, ํšจ์œจ์„ฑํ…Œ์ŠคํŠธ(์‹œ๊ฐ„๋ณต์žก๋„)์—์„œ ์ „๋ถ€ fail์„ ๋ฐ›์•˜๋‹ค. ์™œ์ผ๊นŒ?

์ด ๋ฌธ์ œ์—์„œ๋Š” ๋‘๊ฐ€์ง€์˜ ์—ฐ์‚ฐ์ด ์ˆ˜ํ–‰๋œ๋‹ค. N+1๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด, +1 ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๊ฐ™๋‹ค๋ฉด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ Max๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด์•ผํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•ต์‹ฌ์€, ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ถ€๋ถ„์—์„œ ๋ฐœ์ƒํ–ˆ๋‹ค.

๋‚˜๋Š” ์—ญ์‹œ๋‚˜, answer๋ฐฐ์—ด์„ ์ƒˆ๋กญ๊ฒŒ max๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ, ๋ฐฐ์—ด์ด ์•„์ฃผ ์ปค์งˆ ๊ฒฝ์šฐ, ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ต‰์žฅํžˆ ์ปค์งˆ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋๋‹ค. ์Œ ์˜ˆ๋ฅผ๋“ค์–ด ํ˜„์žฌ ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N+M)์ธ๋ฐ, ๋งŒ์•ฝ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 10000์ผ๊ฒฝ์šฐ, for๋ฌธ ๋‚ด์—์„œ ์ตœ๋Œ€ 10000๋ฒˆ์˜ ๋ฐฐ์—ด๊ฐฑ์‹ ์ด ์ด๋ฃจ์–ด์ง„๋‹ค. fo๋ฌธ์„ ์ค„์ผ ์ˆ˜๋Š” ์—†๊ณ , ์ด ์—ฐ์‚ฐ์„ ์ตœ์†Œํ™”ํ•  ์ˆœ ์—†์„๊นŒ?

(์ธํ„ฐ๋„ท์˜ ๋„์›€์„ ์กฐ๊ธˆ ๋ฐ›์•˜๋‹คใ…œใ…œ ์•„์ง ๋„ˆ๋ฌด ๋ฉ์ฒญ,,ํ•ด,, ํ‘) max์—ฐ์‚ฐ์„ ์ตœ์†Œํ™”ํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, max๊ฐ’์„ ๋‹ค๋ฅธ๊ณณ์— ์ €์žฅํ•ด๋‘๊ณ , ํ•„์š”ํ• ๋•Œ๋งŒ max๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉด ์–ด๋–จ๊นŒ! ์ด ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋ด์•ผํ•œ๋‹ค. 

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งฐ๋‹ค. ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ํ’€์–ด๋ณด์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. currentMax: N+1์ผ ๋•Œ, ๋ฐ”๊ฟ”์ค„ ํ˜„์žฌ ๊ฐ€์žฅ ํฐ max ๊ฐ’์ด๋‹ค.
  2. nextMax: +1ํ–ˆ์„๋•Œ ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ๊ฐ€์žฅ max๊ฐ’์ด๋ผ๋ฉด, nextMax์— ์ž ๊น ๋„ฃ์–ด์ค€๋‹ค.
  3. for๋ฌธ
    1. N+1์ด ์•„๋‹ˆ๋ผ๋ฉด,  +1 ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
    2. ํ˜„์žฌ ๊ฐ’์ด currentMax๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, max๊ฐ’์ด ์•„๋‹ˆ๋‹ˆ๊นŒ max๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
    3. ๊ทธ๋ฆฌ๊ณ  +1 ํ•ด์ค€๋‹ค.
    4. ๋งŒ์•ฝ, ํ•ด๋‹น ๊ฐ’์ด nextMax๋ณด๋‹ค์ž‘์œผ๋ฉด, currentMax๊ฐ’์ด ๋  ๋ช…๋ถ„์ด ์ถฉ๋ถ„ํžˆ ๋˜๋‹ˆ๊นŒ, next์— ์ž ๊น ๋„ฃ์–ด์ค€๋‹ค.
    5. N+1 ์ด๋ผ๋ฉด
    6. currentMax์— nextMax๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด์„œ, max๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.
  4. ๋งˆ์ง€๋ง‰์— max๊ฐ’์œผ๋กœ ๋ณ€ํ•˜์ง€ ์•Š์€ ๊ฒƒ๋“ค์„ currentMax์™€ ๋น„๊ตํ•ด์„œ ๋ฐ”๊ฟ”์ค€๋‹ค.
public func solution(_ N : Int, _ A : inout [Int]) -> [Int] {
    var answer = Array(repeating: 0, count: N)
    var currentMax = 0
    var nextMax = 0

    for a in A {
        // N+1 ์ด ์•„๋‹ˆ๋ผ๋ฉด
        if a != N+1 {
            // +1 ์—ฐ์‚ฐํ•ด์ค˜์•ผํ•ด
            // ๊ทธ์ „์— currentmax๊ฐ’์ด ์กด์žฌํ•œ๋‹ค๋ฉด, ๊ทธ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ 
            // (max๊ฐ’๋ณด๋‹ค ์ž‘์„๊ฒฝ์šฐ, ๋ฐ”๊ฟ”์ค˜, ๋งŒ์•ฝ ํฌ๋ฉด? ์ด๋ฏธ +1์”ฉ ๋”ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๋œป
            if answer[a-1] <= currentMax {
                answer[a-1] = currentMax
            }

            answer[a-1] += 1

            // ๋งŒ์•ฝ ์ง€๊ธˆ +1 ํ•ด์ค€ ๊ฐ’์ด max๊ฐ€ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค๋ฉด
            // nextmax์— ๋„ฃ์–ด์ค˜,,, (๋‹ค์Œ์— ๋ฐ”๊ถˆ์ค„๋•Œ ์‚ฌ์šฉ)
            if answer[a-1] > nextMax {
                nextMax = answer[a-1]
            }
        } else {
            currentMax = nextMax
        }
    }

    // ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ง€์ง€ ์•Š์€ ๊ฒƒ๋“ค ๋ฐ”๊ฟ”์ฃผ๊ธฐ
    for idx in 0..<answer.count {
        if answer[idx] < currentMax {
            answer[idx] = currentMax
        }
    }

    return answer
}
print(solution(N, &A))
๋ฐ˜์‘ํ˜•