๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ Computer Science

[Swift ๋ฐฑ์ค€]1620๋ฒˆ_Dictionary VS Array

by MINT09 2024. 1. 27.

๋ฐฑ์ค€ ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค ์ƒ๊ธด ์ผ์ด๋‹ค. 

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

 

1620๋ฒˆ: ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ

์ฒซ์งธ ์ค„์—๋Š” ๋„๊ฐ์— ์ˆ˜๋ก๋˜์–ด ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ๊ฐœ์ˆ˜ N์ด๋ž‘ ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ ธ. N๊ณผ M์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ธ๋ฐ, ์ž์—ฐ์ˆ˜๊ฐ€ ๋ญ”์ง€๋Š” ์•Œ์ง€? ๋ชจ๋ฅด๋ฉด

www.acmicpc.net

1. Array๋งŒ ์‚ฌ์šฉ (์‹œ๊ฐ„ ์ดˆ๊ณผ)

๋งจ ์ฒ˜์Œ์—๋Š” Array๋งŒ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. Index๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฒˆํ˜ธ๋ฅผ ๊ตฌ๋ถ„ํ•  ์ƒ๊ฐ์ด์—ˆ๋‹ค. 

func ๋‚˜๋Š”์•ผ_ํฌ์ผ“๋ชฌ๋งˆ์Šคํ„ฐ_์ด๋‹ค์†œ() {
    let input = readLine()!.split(separator: " ").compactMap { Int($0) }
    var pokedex = [String]()
    var answer = ""
    
    for _ in 1...input[0] {
        let pokemon = readLine()!
        
        pokedex.append(pokemon)
    }
    
    for _ in 1...input[1] {
        let question = readLine()!
        
        if let number = Int(question) {
            answer += "\(pokedex[number - 1])\n"
        } else {
            let pokemonIndex = pokedex.firstIndex(of: question)
            
            answer += "\(pokemonIndex! + 1)\n"
        }
    }
    
    print(answer)
}

๋‚˜๋Š”์•ผ_ํฌ์ผ“๋ชฌ๋งˆ์Šคํ„ฐ_์ด๋‹ค์†œ()

๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ํ–ˆ๋”๋‹ˆ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค!

firstIndex๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ O(n)์ด๋ผ ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค. 

2. Dictionary 2๊ฐœ ์‚ฌ์šฉ (๋งž์•˜์Šต๋‹ˆ๋‹ค, 256ms)

Dictionary๋‚˜ Set ๋“ฑ HashValue๋กœ ๊ฐ’์„ ์ฐพ๋Š” ๊ฒฝ์šฐ๋Š” Array๋ณด๋‹ค ์กฐ๊ธˆ ๋” ๋น ๋ฅด๋‹ค. ๊ตณ์ด ์ „๋ถ€ ๋Œ์•„๋ณผ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

func ๋‚˜๋Š”์•ผ_ํฌ์ผ“๋ชฌ๋งˆ์Šคํ„ฐ_์ด๋‹ค์†œ() {
    let input = readLine()!.split(separator: " ").compactMap { Int($0) }
    var numberPokedex = [Int: String]()
    var namePokedex = [String: Int]()
    var answer = ""
    
    for i in 1...input[0] {
        let pokemon = readLine()!
        
        numberPokedex[i] = pokemon
        namePokedex[pokemon] = i
    }
    
    for _ in 1...input[1] {
        let question = readLine()!
        
        if let number = Int(question) {
            answer += "\(String(describing: numberPokedex[number]!))\n"
        } else {
            
            answer += "\(String(describing: namePokedex[question]!))\n"
        }
    }
    
    print(answer)
}

๋‚˜๋Š”์•ผ_ํฌ์ผ“๋ชฌ๋งˆ์Šคํ„ฐ_์ด๋‹ค์†œ()

๊ทธ๋Ÿฌ๋‚˜ value๊ฐ’์œผ๋กœ key ๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์€ hashValue๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ์— ๋ฐ˜๋Œ€๋˜๋Š” ๊ฒƒ๊นŒ์ง€ Dictionary๋ฅผ ๋‘ ๊ฐœ ๋งŒ๋“ค์–ด ๊ตฌ๋ถ„ํ•˜์˜€๋‹ค.

ํ•˜์ง€๋งŒ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ ๊ณ ๋ฏผํ–ˆ๋‹ค.

3. Dictionary์™€ Array์˜ ์‚ฌ์šฉ (๋งž์•˜์Šต๋‹ˆ๋‹ค, 204ms)

์ด๋ฆ„์„ key, ๋ฒˆํ˜ธ๋ฅผ value๋กœ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์–ด์ฉ” ์ˆ˜ ์—†์ง€๋งŒ, ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋Š” array์˜ subscript๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

func ๋‚˜๋Š”์•ผ_ํฌ์ผ“๋ชฌ๋งˆ์Šคํ„ฐ_์ด๋‹ค์†œ() {
    let input = readLine()!.split(separator: " ").compactMap { Int($0) }
    var numberPokedex = [String]()
    var namePokedex = [String: Int]()
    var answer = ""
    
    for i in 1...input[0] {
        let pokemon = readLine()!
        
        numberPokedex.append(pokemon)
        namePokedex[pokemon] = i
    }
    
    for _ in 1...input[1] {
        let question = readLine()!
        
        if let number = Int(question) {
            answer += "\(numberPokedex[number - 1])\n"
        } else {
            answer += "\(namePokedex[question]!)\n"
        }
    }
    print(answer)
}

๋‚˜๋Š”์•ผ_ํฌ์ผ“๋ชฌ๋งˆ์Šคํ„ฐ_์ด๋‹ค์†œ()

๊ทธ๋žฌ๋”๋‹ˆ ์‹œ๊ฐ„์ด ํ›จ์”ฌ ์ค„์–ด๋“ค์—ˆ๋‹ค. ์ฐพ์•„์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” subscript๋ฅผ ํ†ตํ•œ ์ ‘๊ทผ์ด ํ›จ์”ฌ ๋น ๋ฅธ ๊ฒƒ ๊ฐ™๋‹ค. 

++ func ์—†์ด ์‹คํ–‰ํ•˜๋ฉด 188ms๊นŒ์ง€ ๋นจ๋ผ์ง„๋‹ค!