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

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๊ฐ์ž ๐Ÿฅ” 2022. 4. 29. 22:31
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ ๋งํฌ

https://programmers.co.kr/learn/courses/30/lessons/92334

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๋ฌธ์ œ ์„ค๋ช… ์‹ ์ž…์‚ฌ์› ๋ฌด์ง€๋Š” ๊ฒŒ์‹œํŒ ๋ถˆ๋Ÿ‰ ์ด์šฉ์ž๋ฅผ ์‹ ๊ณ ํ•˜๊ณ  ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์ง€๊ฐ€ ๊ฐœ๋ฐœํ•˜๋ ค๋Š” ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ์œ ์ €๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜

programmers.co.kr

 

๋ฌธ์ œ ํ’€์ด

์šฐ์„  ์‹ ๊ณ  ๋‹นํ•œ์‚ฌ๋žŒ์„ ๊ธฐ์ค€์œผ๋กœ ์‹ ๊ณ  ํ•œ์‚ฌ๋žŒ์—๊ฒŒ ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— dictionary๋ฅผ ์„ค์ •ํ•  ๋•Œ "์‹ ๊ณ ๋‹นํ•œ์‚ฌ๋žŒ์„ Key"๋กœ ๋‘๊ณ , "์‹ ๊ณ  ํ•œ ์‚ฌ๋žŒ์„ Value"๋กœ ๋‘์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ์„ ์–ธํ•˜ ๋ณ€์ˆ˜๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • reported_report ๋”•์…”๋„ˆ๋ฆฌ : ์‹ ๊ณ ๋‹นํ•œ์‚ฌ๋žŒkey, ์‹ ๊ณ ํ•œ์‚ฌ๋žŒvalue
  • ์–ผ๋งˆ๋‚˜ ์‹ ๊ณ ๋ฅผ ๋‹นํ–ˆ๋Š”์ง€ ์„ธ์–ด์ค„ count ๋”•์…”๋„ˆ๋ฆฌ → ์ด๊ฒŒ ๊ณง ์ •๋‹ต
  • ์ฃผ์–ด์ง„ report ๋Š” "์‹ ๊ณ ํ•œ์‚ฌ๋žŒ ์‹ ๊ณ ๋‹นํ•œ์‚ฌ๋žŒ"์˜ ํ˜•ํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜๋ˆ„์–ด์„œ ๋”•์…”๋„ˆ๋ฆฌ์˜ key์™€ value ๋กœ ๊ฐ๊ฐ ์ €์žฅํ•ด์ค„ ๊ฒƒ์ž„

์ด๊ฒƒ์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์•„, ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ์ค‘์š”ํ•œ์‚ฌ์‹ค ์ค‘ ํ•˜๋‚˜๋Š”, ๋‚ด๊ฐ€ A๋ผ๋Š”์‚ฌ๋žŒ์„ 6๋ฒˆ ์‹ ๊ณ ํ–ˆ๋”๋ผ๋„, count๋Š” 1๋งŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ฐ”๋กœ ์ด๊ฑด ์ค‘๋ณต์ด ์•ˆ๋œ๋‹ค๋Š” ์‚ฌ์‹ค! ๊ทธ๋Ÿผ Array ๊ฐ€ ์•„๋‹ˆ๋ผ set์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์ค˜์•ผํ•œ๋‹ค. ๊ทธ๋ž˜์„œ reported_report ์˜ value๋Š” set์œผ๋กœ ์„ค์ •ํ–ˆ๋‹ค.

1. ์ž…๋ ฅ๋ฐ›์€ ๊ฒƒ๋“ค์„ ๋ณ€์ˆ˜๋กœ ์ •๋ฆฌ

var reported_report: [String:Set<String>] = [:]
var idIdx:[String:Int] = [:]
var count = Array(repeating: 0, count: id_list.count)

 

2. report๋ฅผ ์ชผ๊ฐœ์„œ ๊ฐ์ž์˜ ์œ„์น˜์˜ dic์— ์ €์žฅ

for (i,id) in id_list.enumerated() {
        idIdx[id] = i
}

for r in report {
    let split = r.split(separator: " ")
    let reported = String(split[1])
    let report = String(split[0])

    if reported_report[reported] == nil {
        reported_report[reported] = [report]
    } else {
        reported_report[reported]!.insert(report)
    }
}

 

3. ์ฃผ์–ด์ง„ K์™€ ๋น„๊ตํ•˜์—ฌ count

for r in reported_report {
        if r.value.count >= k {
            for i in r.value {
                count[idIdx[i]!] += 1
            }
        }
    }

 

 

์ตœ์ข… ์ฝ”๋“œ

func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {

    var reported_report: [String:Set<String>] = [:]
    var idIdx:[String:Int] = [:]
    var count = Array(repeating: 0, count: id_list.count)
    
    for (i,id) in id_list.enumerated() {
            idIdx[id] = i
        }
    
    for r in report {
        let split = r.split(separator: " ")
        let reported = String(split[1])
        let report = String(split[0])
        
        if reported_report[reported] == nil {
            reported_report[reported] = [report]
        } else {
            reported_report[reported]!.insert(report)
        }
    }
    
    for r in reported_report {
        if r.value.count >= k {
            for i in r.value {
                count[idIdx[i]!] += 1
            }
        }
    }
    return count
}
๋ฐ˜์‘ํ˜•