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

2022. 10. 5. 16:15ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

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

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

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

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

programmers.co.kr

 

 

 ๋‚˜์˜ ํ’€์ด

 

๋‹ ์ฝ”์–ผ๋ ˆ์‹ฑ(nil์ผ๋–„ ?? ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๊ธฐ๋ณธ๊ฐ’์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ)์— ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ if let์„ ๋„๋ฐฐํ•œ ํ”์ ์ด ๋ณด์ธ๋‹ค..

 

import Foundation

func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
    
    var dictionaryReport: [String: [String]] = [:]
    var checkReported: [String: Int] = [:]
    
    for comp in report {
        let people = comp.components(separatedBy: " ")
        let reporter = people[0]
        let reportedPerson = people[1]
        if let res = dictionaryReport[reporter]?.contains(reportedPerson)
        {
            if res != false {
                continue
            }
        }
        
        
        if let res = dictionaryReport[reporter] {
            dictionaryReport[reporter]!.append(reportedPerson)
        } else {
            dictionaryReport[reporter] = [reportedPerson]
        }
        
        if let res = checkReported[reportedPerson] {
            checkReported[reportedPerson]! += 1 
        } else {
            checkReported[reportedPerson] = 1    
        }
    }
    
    return id_list.map { 
        if let arr = dictionaryReport[$0] {
            let cnt = arr.filter{ checkReported[$0]! >= k }.count
            return cnt
        }
        return 0
    }
}

 

if-let ๋ถ€๋ถ„๋งŒ ๋‹ ์ฝ”์–ผ๋ ˆ์‹ฑ์œผ๋กœ ๋ฐ”๊ฟ”๋„ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค.

 

import Foundation

func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
    
    var dictionaryReport: [String: [String]] = [:]
    var checkReported: [String: Int] = [:]
    
    for comp in report {
        let people = comp.components(separatedBy: " ")
        let reporter = people[0]
        let reportedPerson = people[1]
        if let res = dictionaryReport[reporter]?.contains(reportedPerson)
        {
            if res != false {
                continue
            }
        }
        // ๋ณ€๊ฒฝ๋œ ์ฝ”๋“œ
        dictionaryReport[reporter] = (dictionaryReport[reporter] ?? []) + [reportedPerson]
        checkReported[reportedPerson] = (checkReported[reportedPerson] ?? 0) + 1
    }
    
    return id_list.map { 
        if let arr = dictionaryReport[$0] {
            let cnt = arr.filter{ checkReported[$0]! >= k }.count
            return cnt
        }
        return 0
    }
}

 

 

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

๋ฐฐ์šธ๊ฒŒ ๋งŽ์€ ์ฝ”๋“œ์ด๋‹ค. ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ ์ฝ”์–ด๋ ˆ์‹ฑ์„ ์‚ฌ์šฉํ–ˆ๊ณ  reduce๋ฅผ ์ด์šฉํ•ด์„œ ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ์ด ์–ด๋–ค ์‚ฌ๋žŒ์„ ์‹ ๊ณ ํ–ˆ๋Š”์ง€, ๊ทธ ์‚ฌ๋žŒ์€ ๋ช‡๋ฒˆ ์ง€๋ชฉ๋˜์—ˆ๋Š”์ง€๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜์…จ๋‹ค.

 

import Foundation

func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
    
    var reported: [String: Int] = [:]
    var user: [String: [String]] = [:]
    
    for r in Set(report) {
        let results = r.components(separatedBy: " ")
        user[results[0]] = (user[results[0]] ?? []) + [results[1]]
        reported[results[1]] = (reported[results[1]] ?? 0) + 1
    }
    
    return id_list.map { id in
        return (user[id] ?? []).reduce(0) {
            $0 + ((reported[$1] ?? 0) >= k ? 1 : 0)
        }
    }
}

 

 ํ”ผ๋“œ๋ฐฑ

 

1. if-let ๋ฐ”์ธ๋”ฉ์€ ๊ฐ’์ด nil์ด ์•„๋‹Œ ๊ฒฝ์šฐ์— ์ฝ”๋“œ ๋ธ”๋ก์— ๋“ค์–ด์˜ค๊ฒŒ ๋œ๋‹ค. ์ฆ‰ false๋ผ๋Š” ๊ฐ’๋„ ์ฝ”๋“œ๋ธ”๋ก์— ๋“ค์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— if์™€ ํ˜ผ๋™ํ•˜์ง€ ๋ง์ž.

2. if-let ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋‹ ์ฝ”์–ผ๋ ˆ์‹ฑ์œผ๋กœ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•œ์ง€ ๊ณ ๋ฏผํ•ด๋ณธ๋‹ค.

3. reduce๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์—ด์„ ์ˆซ์ž๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ์— ๊ฐ’์ด ์—†์„ ๋•Œ๋Š” ๋นˆ ๋ฐฐ์—ด์„ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋‘ฌ์„œ ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.