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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 11501๋ฒˆ - ์ฃผ์‹ (์‹ค๋ฒ„2, ๊ทธ๋ฆฌ๋””)

๊ฐ์ž ๐Ÿฅ” 2023. 4. 25. 18:55
๋ฐ˜์‘ํ˜•

์‹ค๋ฒ„2์—ฌ์„œ ์‰ฌ์šด ๋ฌธ์ œ์˜€์ง€๋งŒ, ์™œ ์‹ค๋ฒ„2์•ผ? ๋ผ๋Š” ๋ง์ด ํ•œ๋ฒˆ ํˆญ ํŠ€์–ด๋‚˜์™”๋‹ค ใ…Žใ…Ž
์งง์ง€๋งŒ ๊ทธ๋ž˜๋„ ๊ณ ๋ฏผ์„ ์กฐ๊ธˆ ํ–ˆ๋˜ ๋ฌธ์ œ๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์„ฑํ•ด๋†“์Œ! 
์‹ค๋ฒ„2๋‹ต๊ฒŒ, ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ฆฌ๋”” ๋‹ต๊ฒŒ ํ’€์ด๋ฅผ ์ƒ๊ฐํ•ด๋‚ด๋Š” ์ˆœ๊ฐ„ ๊ทธ๋‹ฅ ์–ด๋ ต์ง€๋Š” ์•Š์•˜๋‹ค~

 

โšซ๏ธ ๋ฌธ์ œ

https://www.acmicpc.net/problem/11501

 

11501๋ฒˆ: ์ฃผ์‹

์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ž์—ฐ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๋ณ„๋กœ ์ฒซ ์ค„์—๋Š” ๋‚ ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ž์—ฐ์ˆ˜ N(2 ≤ N ≤ 1,000,000)์ด ์ฃผ์–ด์ง€๊ณ , ๋‘˜์งธ ์ค„์—๋Š” ๋‚  ๋ณ„ ์ฃผ๊ฐ€๋ฅผ ๋‚˜ํƒ€

www.acmicpc.net

 

โšซ๏ธ ํ’€์ด

1๏ธโƒฃ ์ฒซ๋ฒˆ์งธ ์‹œ๋„

์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ์ œ์ถœ ์‹œ๊ฐ„ ๋ณด๋ฉด 1๋ถ„์ „์ด๋ผ๊ณ  ๋œจ๋Š”๋ฐ ใ…‹ใ…‹ 1๋ถ„๋™์•ˆ ์ฒœ์ฒœํžˆ ์ฑ„์ ํ•˜๋”๋‹ˆ 88%์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ์•„๋ฌด๋ž˜๋„ ๋ฐ์ดํ„ฐ๊ฐ€ N์ด 100๋งŒ๊นŒ์ง€ ๋“ค์–ด์˜ค๋ฉด ๋ป‘๋‚˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

์•„๋‹ˆ, ๊ทผ๋ฐ ์ด๊ฑฐ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์€๊ทผ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค. ์ผ๋‹จ ํ’€์ด๊ณผ์ •์„ ์ข€ ๋ณด์ž

๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์ฐพ์€ ์ฒซ๋ฒˆ์žฌ ํ’€์ด ๋ฐฉ๋ฒ•์ด๋‹ค. ์ฃผ์‹์€ ํ•œ๊ฐœ์”ฉ๋งŒ ์‚ด ์ˆ˜ ์žˆ์ง€๋งŒ ํŒŒ๋Š”๊ฑด ๋ง˜๋Œ€๋กœ ํŒ” ์ˆ˜ ์žˆ๋‹ค. ์ผ๋‹จ ๋ช‡๊ฐœ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋“  ์ตœ๊ณ  ๊ฐ€๊ฒฉ์œผ๋กœ ํŒŒ๋Š”๊ฒŒ +1 ์ด๋ผ๋„ ์ด๋“์„ ๋ณด๊ธฐ ๋•Œ๋ฌธ์—, max๊ฐ’์„ ์ฐพ์•„์ฃผ์ž๊ณ  ํŒ๋‹จํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ๋งŒ์•ฝ max๊ฐ’์ด ์ค‘๊ฐ„์— ์žˆ๋‹ค๋ฉด, ๊ทธ ์ดํ›„์—๋Š” ๋˜ ์ฃผ์‹์„ ์‚ด์ง€ ํŒ”์ง€ ๊ณ ๋ฏผํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ์ง€๊ธˆ ์ฐพ์€ max๊ฐ’ ์ดํ›„์˜ ๊ฐ’๋“ค ์ค‘์—์„œ ๋˜ max๊ฐ’์„ ์ฐพ๊ณ , ํŒ”์•„์ฃผ๊ณ  ... ๐Ÿ” ๋ฅผ ๋ฐ˜๋ณตํ•ด์ฃผ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ? 

์ด๋ก ์ƒ์œผ๋กœ๋Š”, ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ๊ฒฐ๊ตญ max๊ฐ’์„ ์ฐพ๊ณ  ๋ฐฐ์—ด์„ ์ž˜๋ผ์„œ ๋˜ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„ ํ˜•ํƒ์ƒ‰๊ณผ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฐ™์„ ๊ฒƒ์ด๋ผ๊ณ  ํŒ๋‹จํ–ˆ๋‹ค. N์€ ์ด 100๋งŒ๊ฐœ๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์„ ํ˜•ํƒ์ƒ‰์˜ ์‹œ๊ฐ„๋ณต์žก๋„์ธ O(n) ๊ณผ ๋™์ผํ• ๊ฒƒ์ด๋ผ๊ณ  ํŒ๋‹จํ–ˆ๋‹ค. 

๊ทธ๋ž˜์„œ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ฒ˜์Œ์— ์žฌ๊ท€๋กœ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ๊ตฌํ˜„์ด ์•ˆ๋˜๊ธธ๋ž˜ (๋‚ด ๋จธ๋ฆฌ์˜ ํ•œ๊ณ„.. ์™œ ์•ˆ๋์ง€? ใ…‹ใ…‹ใ…‹ใ…‹ ์ผ๋‹จ ๋น ๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” while๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ–‡๋‹ค.

import Foundation

func solution() {
    let t = Int(readLine()!)!

    for _ in 0..<t {
        let n = Int(readLine()!)!
        var price = readLine()!.split(separator: " ").map{ Int($0)! }
        var answer = 0
        var idx = 0

        while idx < price.count {
            if price.count == 1 {
                break
            }

            let maxNum = price.max()!
            idx = price.firstIndex(of: maxNum)!

            for i in 0..<idx {
                if maxNum > price[i] {
                    answer += maxNum - price[i]
                }
            }

            if idx == price.count - 1 {
                break
            }

            price = Array(price[idx+1..<price.count])
            idx = 0
        }

        print(answer)
    }
}

solution()

 

์Œ.. O(n)์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ํ•ด๋†จ์ง€๋งŒ, ์–ผ๋–จ๊ฒฐ์— ๊ตฌํ˜„์„ ๋งˆ๊ตฌ์žก์ด๋กœ ํ•˜๋‹ค๋ณด๋‹ˆ O(n^2)์œผ๋กœ ๊ตฌํ˜„ํ•ด๋†“์•˜๋”๋ผ๊ณ .. ๊ทธ๋ž˜์„œ์ธ์ง€ 100๋งŒ์ด ๋„˜์–ด๊ฐ€๋ฉด ๋‹น์—ฐํžˆ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ฒ ์ง€ ใ… ใ…  ๋‹ค๋ฅธ ๊ตฌํ˜„๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด์•„์•ผํ•œ๋‹ค. 

2๏ธโƒฃ ๋‘๋ฒˆ์žฌ ์‹œ๋„ - while๋ฌธ์„ ์—†์• ๊ณ  ๊ตฌํ˜„ํ•œ๊ฑด ์•„๋ž˜ ์ฝ”๋“œ.

import Foundation

func solution() {
    let t = Int(readLine()!)!

    for _ in 0..<t {
        let n = Int(readLine()!)!
        var price = readLine()!.split(separator: " ").map{ Int($0)! }
        var answer = 0

        var maxNum = price.max()!

        for i in 0..<price.count {
            if price[i] < maxNum {
                answer += maxNum - price[i]
            } else if price[i] == maxNum {
                if i == price.count - 1 {
                    break
                }
                maxNum = Array(price[i+1..<price.count]).max()!
            }
        }

        print(answer)
    }
}

solution()

์•„ ์ด๊ฒƒ๋„, ์ด์ „ ์ฒซ๋ฒˆ์จฐ ์ฝ”๋“œ์™€ ๋™์ผํ•˜๊ฒŒ 1๋ถ„์ •๋„ ์ฑ„์ ์„ ํ•˜๋”๋‹ˆ 83%์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ใ…‹ใ…‹ใ…‹ใ…‹ ์•„์˜ค ์ฑ„์  ๊ธฐ๋‹ค๋ฆฌ๋Š”๊ฑฐ ์‹ฌ์žฅ๋–จ๋ ค.. ๊ทธ๋‚˜์ €๋‚˜ ์—ฌ๊ธฐ์„œ ๋‚ด๊ฐ€ ๊ฐ„๊ณผํ•œ๊ฒŒ ํ•˜๋‚˜ ์žˆ์—ˆ๋‹ค. ๋ฐ”๋กœ,,, Swift ๋ฐฐ์—ด์—์„œ max()๊ฐ’์„ ์ฐพ๋Š” ๋งค์„œ๋“œ๊ฐ€ ๋ฐ”๋กœ O(N)์ด๋ผ๋Š” ์‚ฌ์‹ค. ใ… ใ… ใ…  ๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ์ด๋ ‡๊ฒŒ ํ’€๋”๋ผ๋„, n๊ฐœ์˜ for๋ฌธ์„ ๋Œ์ง€๋งŒ, for ๋ฌธ ๋‚ด๋ถ€์—์„œ O(N)์”ฉ max๊ฐ’์„ ์ฐพ์•„์ฃผ๋ฏ€๋กœ, O(N^2)์ด ๋œ๋‹ค ...... ํ•˜....... ์ด๋ ‡๊ฒŒ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋ž€ ๋ง์ธ๊ฐ€ ใ… ใ…  

์ง€๋‚œ๋ฒˆ์— ์นœ๊ตฌ๋ž‘ ์ด์•ผ๊ธฐํ• ๋•Œ, ์ด๋ ‡๊ฒŒ ํ’€์—ˆ๋Š”๋ฐ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค๋ฉด, ํ’€์ด ๊ณผ์ • ์ž์ฒด๊ฐ€ ์ž˜๋ชป ๋œ๊ฑฐ๋‹ค ๋ผ๊ณ  ์ด์•ผ๊ธฐ ๋‚˜๋ˆˆ์ ์ด ์žˆ๋Š”๋ฐ..ใ…‹ใ…‹ใ…‹ ๋‚ด๊ฐ€ ํ’€์ดํ•˜๋Š” ๋ฐฉ์‹์ด ์ž˜๋ชป๋๋‚˜ ์‹ถ๋‹ค. ... ํ .. ๋‹ค์‹œ ๊ณ ๋ฏผํ•ด๋ณด์ž. ใ… ใ…  ํ•˜ ... ์‰ฌ์šด๋ฌธ์ œ์ธ๊ฑฐ๊ฐ™์€๋ฐ ์™ค์ผ€ ๊ณ ๋ฏผํ•˜๊ณ ์žˆ๋Š”๊ฑฐ์ง€ ;; ์‹ค๋ฒ„2๋ผ๊ณ  ๊ณ ์ž‘!!!!

 

์•Œ์•˜๋‹ค. ์•Œ์•˜๋‹ค. ์•Œ์•˜๋‹ค!! ๋‚ด๊ฐ€ ํ‘ผ ๋ฐฉ์‹์€ ํ‹€๋ฆฌ์ง€ ์•Š์•˜๊ณ , ๊ตฌํ˜„ํ•˜๋Š”๋ฐ์„œ ์กฐ๊ธˆ ๋” ๊ณ ๋ฏผ์ด ํ•„์š”ํ–ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฃผ์‹์˜ ๊ฐ€๊ฒฉ (๋‚ด๊ฐ€ price๋ผ๊ณ  ๋ณ€์ˆ˜ ๋งŒ๋“ค์–ด๋†“์€๊ฒƒ)์˜ ์ˆœ์„œ๋Š” ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค. ๊ฒฐ๊ตญ, ์ตœ๋Œ€ ์ด์ต์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š”, ๋’ค์—์žˆ๋Š” ์ˆซ์ž๊ฐ€ ์ปค์•ผ, ํŒ”์•„์„œ ์ˆ˜์ต์„ ๋‚จ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ๋ฐฐ์—ด์„ ๊ฑฐ๊พธ๋กœ ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ํ˜„์žฌ -> ๊ณผ๊ฑฐ๋กœ ๋Œ์•„๊ฐ„๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ , ํ˜„์žฌ max๊ฐ€ n์›์ด๋ผ๋ฉด, ์•ž์—์žˆ๋Š”์• ๋ฅผ ์‚ฌ๊ณ ํŒŒ๋Š”๊ฒŒ ์ด๋“์ธ๊ฐ€?๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.

์ด ๊ณผ์ •์„ ๋งจ ๋’ค๋ถ€ํ„ฐ ์•ž๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด๋ณด์ž. ๊ทธ๋ ‡๋‹ค๋ฉด, max๊ฐ’์„ max()!๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฐพ์•„์ค„ ํ•„์š”๋„ ์—†๊ณ , ๊ทธ๋ƒฅ ํ•˜๋‚˜์˜ for๋ฌธ์œผ๋กœ if๋ฌธ๋งŒ์„ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰, O(N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค

3๏ธโƒฃ ์„ธ๋ฒˆ์งธ ์‹œ๋„

์œ„ ๊ทธ๋ฆผ์„ ๊ทธ๋Œ€๋กœ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

import Foundation

func solution() {
    let t = Int(readLine()!)!

    for _ in 0..<t {
        let n = Int(readLine()!)!
        var price = readLine()!.split(separator: " ").map{ Int($0)! }
        var answer = 0

        var maxNum = price[price.count-1] // max์ดˆ๊ธฐ๊ฐ’ ๊ทธ๋ƒฅ ๋งจ๋’ค ๊ฐ’์œผ๋กœ

        for i in stride(from: price.count-1, through: 0, by: -1) {
            if maxNum < price[i] { // maxnum๋ณด๋‹ค ํฐ๋†ˆ์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค๋ฉด max๋ฅผ ๊ฐฑ์‹ ํ•ด์ค˜
                maxNum = price[i]
            } else if price[i] < maxNum {
                answer += maxNum - price[i]
            }
        }

        print(answer)
    }
}

solution()

์งœ์ž”~~ ๋งž์•˜๋‹ค ํ›„,,, ์ด ๋ฌธ์ œํ‘ธ๋Š”๋ฐ ํ•œ์‹œ๊ฐ„ ๋ฐ˜์ •๋„? ๊ฑธ๋ฆฐ๊ฒƒ ๊ฐ™๋‹ค. ์—ญ์‹œ, ํ’€์ด๊ฐ€ ์ƒ๊ฐ๋‚ฌ๋Š”๋ฐ, ๊ตฌํ˜„์ด ์–ด๋ ค์šฐ๋ฉด ์ง„์งœ ,,,, ์ฐพ๊ธฐ๋„ ํž˜๋“ค๊ณ  ๊ทธ๋ ‡๋‹ค ใ… ใ…  ์ค‘๊ฐ„์— index๋ฌธ์ œ๋กœ ์—„์ฒญ ํ—ท๊ฐˆ๋ ค์„œ ํ—ค๋งค๊ธฐ๋„ ํ–ˆ๊ณ , ์žฌ๊ท€๋กœ ๊ตฌํ˜„ํ–‡๋‹ค๊ฐ€ ์–ด์ฉŒ๊ณ  ์ €์ฉŒ๊ณ  ํ•˜๋ฉด์„œ ํ—ˆํƒ•์นœ ์ฝ”๋“œ๋„ ๋งŽ์•˜๋‹ค. ์•„์ง,, ๊ตฌํ˜„์‹ค๋ ฅ์ด ๋งŽ์ด ๋ถ€์กฑํ•œ ๊ฒƒ ๊ฐ™๋‹ค ใ… ใ…  

๋” ์—ด์‹ฌํžˆ ํ’€์–ด์•ผ๊ฒ ๋‹ค. ์•„์ž์•„์ž..... ์‹ค๋ฒ„2์ฃผ์ œ์— ๋‚˜์—๊ฒŒ ๋งŽ์€ ๊ณ ๋ฏผ์„ ํ•˜๊ฒŒ ํ–ˆ๋„ค ...... ํ•˜... ๋ฐฐ์—ด์„ ๊ฑฐ๊พธ๋กœ ๋Œ๋ ค์„œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํ™•!! ์ค„์–ด๋“œ๋Š” ํ’€์ด๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ๋งŽ์€ ๊ฒƒ ๊ฐ™์œผ๋‹ˆ๊นŒ, ์ž˜ ์ƒ๊ฐํ•ด๋‘์ž. 

๋ฐ˜์‘ํ˜•