Focus On Develop ๐ŸคŸ๐ŸคŸ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฒด์œก๋ณต ๋ณธ๋ฌธ

iOS [Swift]/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฒด์œก๋ณต

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2020. 12. 15. 21:07

<๋ฌธ์ œ>


<์ „๋žต์งœ๊ธฐ>

์šฐ์„  ์ œํ•œ์‚ฌํ•ญ์„ ๋ณด๋ฉด ์ „์ฒด Input์˜ ์ˆ˜๋Š” ํฌ์ง€ ์•Š์•„์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๋Š” ํฌ๊ฒŒ ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์ง€๋Š” ์•Š๋‹ค. ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ, ์—ฌ๋ฒŒ์˜ ์ฒด์œก๋ณต์„ ๊ฐ€์ง„ ํ•™์ƒ์ด ๊ฐ๊ฐ ๋ฐ˜๋“œ์‹œ ์กด์žฌํ•˜๋‹ˆ, ์—†๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋”ฐ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™๋‹ค. (๋ณดํ†ต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€๋•Œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๋๋ถ€๋ถ„ ์กฐ๊ฑด(?) ์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•ด์ค˜์•ผ ํ•  ๋ถ€๋ถ„์ด ์ข…์ข… ์ƒ๊ธฐ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.)

reserve๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์—ฌ๋ฒŒ์ด ์žˆ๋Š” ํ•™์ƒ์ด ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ(lost)์—๊ฒŒ ๋นŒ๋ ค์ค€๋‹ค๊ณ  ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€ ๊ฒƒ์ด๋‹ค. ๋จผ์ € reserve์— ์žˆ๋Š” ํ•™์ƒ์ด lost์—๋„ ์žˆ๋Š”์ง€ ์ฒดํฌ (์—ฌ๋ฒŒ์ด ์žˆ์–ด๋„ ๋„๋‚œ๋‹นํ–ˆ์œผ๋ฉด ๋นŒ๋ ค์ค„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ) ํ•˜๊ณ , ๋ณธ์ธ ์•ž -> ๋’ค ์ˆœ์„œ๋กœ lost์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ๋นŒ๋ ค์ฃผ๊ณ , ๋„๋‚œ๋‹นํ•œ ๋ชฉ๋ก์—์„œ ์ง€์›Œ์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค. ์—ฌ๊ธฐ์„œ ๋นŒ๋ ค์ฃผ๋Š” ์ˆœ์„œ๋ฅผ ๋งž์ถฐ์ค˜์•ผ ํ•˜๋Š” ์ด์œ ๋Š”, ์˜ˆ๋ฅผ ๋“ค์–ด์„œ n = 5, lost = [2, 4], reserve = [3, 5] ๋ผ๊ณ  ์ฃผ์–ด์กŒ์„๋•Œ ์ˆœ์„œ๋ฅผ ์ง€ํ‚ค์ง€ ์•Š์œผ๋ฉด 3๋ฒˆ์ด 4๋ฒˆ์—๊ฒŒ ๋Œ€์—ฌ๋ฅผ ํ•ด์ค˜๋ฒ„๋ฆฌ๋ฉด, 5๋ฒˆ์ด ์žˆ์Œ์—๋„ 4๋ฒˆ์—๊ฒŒ ๋Œ€์—ฌ๋ฅผ ํ•ด์ค„ ์ˆ˜ ์—†๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (+ 2๋ฒˆ๋„ ๋Œ€์—ฌ๋ฅผ ๋ชป๋ฐ›๊ฒŒ ๋œ๋‹ค.)

์ „์ฒดํ•™์ƒ์ˆ˜ - ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ์ˆ˜ (lost) + ์ฒด์œก๋ณต์„ ๋นŒ๋ฆฐ ํ•™์ƒ์ˆ˜ ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

<์ฝ”๋“œ>

import Foundation

func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
    var result = n - lost.count
    var lost = lost.sorted()
    var reserve = reserve.sorted()
    
    for item in reserve {
        guard lost.count > 0 else { return result }    
        if lost.contains(item) {
            lost = lost.filter { $0 != item }
            result += 1
        } else if lost.contains(item-1) {
            lost = lost.filter { $0 != item-1 }
            result += 1
        } else if lost.contains(item+1) {
            lost = lost.filter { $0 != item+1 }
            result += 1
        }
    }
    return result
}

 

์‘..? ๊ทธ๋Ÿฐ๋ฐ ๊ฒฐ๊ณผ๊ฐ€..? ๋„๋Œ€์ฒด 5๋ฒˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ๋ญ๊ธธ๋ž˜..?

ํ•˜.. ๊ต‰์žฅํžˆ ๊ธด์‹œ๊ฐ„๋™์•ˆ ์ด๋Ÿฐ ๋‹น์—ฐํ•œ ์˜ˆ์™ธ์ผ€์ด์Šค๋ฅผ ๋ชป์ฐพ์•„์„œ ์‚ฝ์งˆ์„ ํ–ˆ๋‹ค ใ… ใ…  ๐Ÿ˜ญ ๐Ÿ˜ญ ๐Ÿ˜ญ ๐Ÿ˜ญ ๐Ÿ˜ญ

n = 5, lost = [2, 3, 4], reserve = [1, 2, 5] ...... ์ด๋Ÿฐ ์ผ€์ด์Šค์—์„œ๋Š” 2๋ฒˆ์€ ๋Œ€์—ฌ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•ด์•ผ ํ•˜๋Š”๋ฐ ํ•ด๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค....

if lost.contains(item) {
  lost = lost.filter { $0 != item }
  result += 1
}

์ด ๋ถ€๋ถ„์—์„œ ์—ฌ๋ฒŒ์ด ์žˆ์ง€๋งŒ ๋„๋‚œ๋‹นํ•œ ๊ฒฝ์šฐ๋ฅผ ๋‹ค ์žก์•„์ค„๊ฑฐ๋ผ๊ณ  ์‰ฝ๊ฒŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ์•„๋‹ˆ์˜€๋‹ค ใ…Žใ…Ž;

์ด๋Ÿฐ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•œ ์ด์œ ๋Š”, reserve๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋นŒ๋ ค์ค„ ๋Œ€์ƒ์„ ์ฐพ์œผ๋ ค๊ณ  ํ•ด์„œ ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™๋‹ค. (idea thanks to ์‡ผ๋””๋‹ˆ ๐ŸคŸ )

"๋‚˜ ์—ฌ๋ฒŒ์˜ท ์žˆ๋Š”๋ฐ ๋„ˆ ๋นŒ๋ฆด๋ž˜?"๊ฐ€ ์•„๋‹ˆ๋ผ.. "๋‚˜ ๋„๋‚œ๋‹นํ–ˆ๋Š”๋ฐ ๋„ˆ ์—ฌ๋ฒŒ์˜ท ์žˆ๋‹ˆ?" ๋ผ๊ณ  ์ ‘๊ทผํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ.. ์ง€๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋‹น์—ฐํžˆ ๊ทธ๋ ‡๊ฒŒ ์ ‘๊ทผ ํ–ˆ์–ด์•ผ ํ•˜๋Š”๊ฒŒ ๋งž๋‹ค. (ํ•™์ƒ์œผ๋กœ ์ด์ž…ํ•ด์„œ ์ƒ๊ฐํ•ด๋ด๋„ ๊ทธ๋ ‡๋‹ค ใ…‹ใ…‹) ์•”ํŠผ ๊ธฐ์ค€์ด ์ž˜๋ชป๋๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค๊ธด ํ–ˆ๋Š”๋ฐ, ์ฝ”๋“œ ๊ณ ์น˜๊ธฐ๊ฐ€ ๊ท€์ฐฎ์•„์„œ..

์•„์˜ˆ ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ๊ณผ ์—ฌ๋ฒŒ์ด ์žˆ๋Š” ํ•™์ƒ์„ ๋น„๊ตํ•ด์„œ ์ค‘๋ณต๋˜๋Š” ์นœ๊ตฌ๋“ค์€ ๋ชจ๋‘ ๋‚ ๋ฆฌ๊ณ  ์‹œ์ž‘ํ–ˆ๋”๋‹ˆ ํ†ต๊ณผ.. ใ…‹ใ…‹ ๐Ÿ‘

import Foundation

func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
    var realLost = lost.filter { num -> Bool in
        return !reserve.contains(num)
    }.sorted()
    
    let realReserve = reserve.filter { num -> Bool in
        return !lost.contains(num)
    }.sorted()
    
    var result = n - realLost.count
    
    for item in realReserve {
        guard realLost.count > 0 else { return result }
        
        if realLost.contains(item-1) {
            realLost = realLost.filter { $0 != item-1 }
            result += 1
        } else if realLost.contains(item+1) {
            realLost = realLost.filter { $0 != item+1 }
            result += 1
        }
    }
    return result
}

<Lessons Learned>

๋ฌธ์ œ ๋ณด๊ณ  ์Œ ๊ทธ๋ƒฅ ํ•˜๋ฉด ๋˜๊ฒ ๋Š”๋ฐ? ์‰ฌ์šด๋ฐ? ๋ผ๊ณ  ์ ‘๊ทผํ–ˆ๋Š”๋ฐ..ใ…‹ใ…‹ใ…‹ใ…‹ ์ฒ˜์Œ์— ์ „๋žต์„ ์„ธ์šธ๋•Œ ๊ธฐ์ค€์„ ์žก๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ๋” ์‹ ์ค‘ํžˆ ์ƒ๊ฐํ•ด๋ณด๊ณ  ํ•ด์•ผ๊ฒ ๋‹ค. ์ด๋Ÿฌ๋‚˜ ์ €๋Ÿฌ๋‚˜ ๋˜‘๊ฐ™๊ฒ ์ง€ ํ–ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ์•„๋‹Œ ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.. ์‹œ๊ฐ„์œผ๋กœ ์ƒ๊ฐํ•ด๋ณผ ๊ฑฐ๋ฆฌ๋ฅผ ์ƒ€๋‹ค๊ณ  ์น˜์ž ... ๐Ÿ˜ 

 

์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์‚ฌ์šฉ๋œ filter ๋‚ด์šฉ์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด?  ๐Ÿ‘‰  2020/12/15 - [iOS/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!] - [Swift] map, filter, reduce

 

 

์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฒด์œก๋ณต

Comments