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

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฒด์œก๋ณต (lv.1 ๊ทธ๋ฆฌ๋””)

๊ฐ์ž ๐Ÿฅ” 2023. 2. 2. 17:35
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42862

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐ŸŸ  ์ฒซ๋ฒˆ์งธ ํ’€์ด

์•„๋ฌด๊ฒƒ๋„ ๊ณ ๋ คํ•˜์ง€ ์•Š์€ ์ฑ„ if ๋ฌธ์„ ๊ฐˆ๊ฒจ์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋”๋‹ˆ 3, 5, 7, 12, 24 ๋‹ค์„ฏ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์—์„œ ํ‹€๋ ธ๋‹ค ใ…Žํ—คํ—คํ—ค ๋ฌธ์ œ ๋ณด์ž๋งˆ์ž ๊ทธ๋ƒฅ if๋ฌธ์„ ๊ฐˆ๊ฒผ๋Š”๋ฐ! ์—ญ์‹œ๋‚˜ ๋ญ ํ•˜๋‚˜ ์ƒ๊ฐ์ด ๋น ์ง„ ๊ฒƒ ๊ฐ™๋‹ค.

๊ฐ€์žฅ ๋จผ์ € ์ƒ๊ฐํ–ˆ๋˜๊ฒƒ์€, ๊ทธ๋ƒฅ ์ž…๋ ฅ๋ฐ›์€ ์ „์ฒด ํ•™์ƒ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ, lost์— ์กด์žฌํ•˜๋Š”๋ฐ ์˜ท์„ ๋นŒ๋ ค์ค„ ์‚ฌ๋žŒ์ด ์—†๋‹ค๋ฉด -1 ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.

โŒ ํ‹€๋ฆฐ ์ฝ”๋“œ

func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
    var total = n
    var reserve = reserve

    for num in 1...n {
        if lost.contains(num) {
            if reserve.contains(num-1) {
                reserve.remove(at: reserve.firstIndex(of: num-1)!)
            } else if reserve.contains(num+1) {
                reserve.remove(at: reserve.firstIndex(of: num+1)!)
            } else {
                total -= 1
            }
        }
    }
    return total
}

 

โŒ 5๋ฒˆ 12๋ฒˆ ํ…Œ์ผ€ ํ‹€๋ฆผ!! 

์•„,, ์กฐ๊ฑดํ•˜๋‚˜๋ฅผ ๋นผ๋จน์—ˆ๋‹ค. ์ฒด์œก๋ณต ์—ฌ๋ฒŒ์„ ๊ฐ€์ง„ ์นœ๊ตฌ๊ฐ€ ์ฒด์œก๋ณต์„ ๋„๋‘‘๋งž์•˜์„ ๊ฒฝ์šฐ, ๋ณธ์ธ์ด ๊ทธ๊ฑธ ์ž…์–ด์•ผํ•ด์„œ ๋”์ด์ƒ reserve ๋ฐฐ์—ด์— ๋‚จ์•„์žˆ์„ ์ˆ˜ ์—†๋‹ค. ์ด๋ถ€๋ถ„์„ ์ฒดํฌํ•ด์ฃผ์ง€ ์•Š์•˜๋‹ค. ๋ฐ”๋กœ ๊ตฌํ˜„ ๊ณ  ํ–ˆ์ง€๋งŒ? ํ‹€๋ ธ๊ณ ์š”?

func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
    var total = n
    var lost = lost
    var reserve = reserve

    for num in 1...n {
        if lost.contains(num), reserve.contains(num) {
            lost.remove(at: lost.firstIndex(of: num)!)
            reserve.remove(at: reserve.firstIndex(of: num)!)
            continue
        }

        if lost.contains(num) {
            if reserve.contains(num-1) {
                reserve.remove(at: reserve.firstIndex(of: num-1)!)
            } else if reserve.contains(num+1) {
                reserve.remove(at: reserve.firstIndex(of: num+1)!)
            } else {
                total -= 1
            }
        }
    }
    return total
}

 

โœ… ์ž, ์„ธ๋ฒˆ์งธ ์‹œ๋„

๋ญ๊ฐ€ ํ‹€๋ ค!!!!!!! 
์ผ๋‹จ, ๊ฐ™์€ ์• ๋“ค์„ ํ•˜๋‚˜์˜ for๋ฌธ ๋‚ด๋ถ€์—์„œ ๋Œ๋ฆฌ๊ฒŒ๋˜๋ฉด lost ๊ฐ€ ใ„ฑใ…–์† ๋ณ€ํ˜•๋˜๊ณ , ๊ฒ€ํ† ํ•˜์ง€ ์•Š์•„๋„ ๋  lost์• ๋“ค์ด ์กฐ๊ฑด๋ฌธ์„ ํ†ต๊ณผํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ํ•˜๋‚˜์˜ for๋ฌธ์„ ๋‘๊ฐœ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์ž‘์„ฑํ•ด์•ผํ–ˆ๋‹ค. ์ฆ‰

  1. lostํ•œ ์• ๋“ค์ด reserve์— ์žˆ์œผ๋ฉด ์ œ๊ฑฐํ•ด์ฃผ๋Š” ์ž‘์—…์ด ์„ ํ–‰๋˜๊ณ 
  2. ๊ทธ๋‹ค์Œ lost ์• ๋“ค์ด ์ฒด์œก๋ณต์„ ๋นŒ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ง€ ํ™•์ธํ•ด์•ผํ•จ

์ด์ˆœ์„œ๋Œ€๋กœ ๊ฐ€์•ผํ–ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๊ณ„์† 13๋ฒˆ๊ณผ 14๋ฒˆ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ํ‹€๋ ธ๋‹ค ใ…‹ใ…‹ใ…‹ใ…‹ ์•„์˜ค๋นก์ณ! ์™œํ‹€๋ฆฌ์ง€ 

์•„๋ž˜ ์˜ˆ์™ธ์‚ฌํ•ญ์„ ์ฐพ์•˜๋‹ค.... for ๋ฃจํ”„๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ๋˜์–ด์žˆ๋‹ค. ๋งŒ์•ฝ, ์ž…๋ ฅ๋ฐ›๋Š” ์ˆ˜๋“ค์ด ์ •๋ ฌ๋˜์–ด ์žˆ์ง€ ์•Š์€ ์ƒํƒœ๋กœ ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด, ์˜ท์„ ์ตœ๋Œ€๋กœ ๋นŒ๋ ค์ค„์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.  (์•„๋ž˜ ํ•„๊ธฐ๋ฅผ ๋ณด์ž)

์ด๋Ÿฐ ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค. lost๋ฅผ [2, 4]๋กœ ์ •๋ ฌํ•˜๋ฉด, 3๋ฒˆ์ด 2์—๊ฒŒ ๋นŒ๋ ค์ฃผ๊ณ , 5๋ฒˆ์ด 4์—๊ฒŒ ๋นŒ๋ ค์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. lost๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ 4๋จผ์ € ํƒ์ƒ‰ํ•˜๊ณ , reserve์—์„œ 3์„ ๋จผ์ € ๊บผ๋‚ด์™€์„œ ์ตœ๋Œ€ ์ธ์›์„ ๊ตฌํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ lost ์™€ reserve๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.  ํœด ๐Ÿ’จ

 

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

func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
    var total = n
    var lost = lost.sorted(by: <)
    var reserve = reserve.sorted(by: <)

    for num in lost {
        if reserve.contains(num) {
            reserve.remove(at: reserve.firstIndex(of: num)!)
            lost.remove(at: lost.firstIndex(of: num)!)
        }
    }

    for l in lost {
        if reserve.contains(l-1) {
            reserve.remove(at: reserve.firstIndex(of: l-1)!)
        } else if reserve.contains(l+1) {
            reserve.remove(at: reserve.firstIndex(of: l+1)!)
        } else {
            total -= 1
        }
    }

    return total
}

 

๋ฐ˜์‘ํ˜•