๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Programmers] ์œ„์žฅ

๋ฌธ์ œ๋งํฌ ๐Ÿ‘‰ https://programmers.co.kr/learn/courses/30/lessons/42578?language=swift

 

๋ฌธ์ œ ์ดํ•ด 

์ŠคํŒŒ์ด๋“ค์ด ๋งค์ผ ๋‹ค๋ฅธ ์˜ท์œผ๋กœ ์œ„์žฅํ•˜๋Š”๋ฐ ๊ทธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹น

๋ฌธ์ œ ํ’€์ด

๋ฌธ์ œ ์ž…์ถœ๋ ฅ ์˜ˆ 1๋ฒˆ์„ ๋ณด๋ฉด, 

headgear๋กœ๋Š” "yellowhat", "green_turban" 2๊ฐœ๊ฐ€ ์žˆ๊ณ 

eyewear๋กœ๋Š” "bluesunglasses" 1๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 

๊ทธ๋ž˜์„œ ์ŠคํŒŒ์ด๊ฐ€ ์˜ท์„ ์ž…๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ๋Š” 

1. yellow_hat
2. blue_sunglasses
3. green_turban
4. yellow_hat + blue_sunglasses
5. green_turban + blue_sunglasses

์ด๋ ‡๊ฒŒ 5๊ฐœ๋ฅผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์–ด์š”. ์˜ท์˜ ์ข…๋ฅ˜๋Š” ํ•œ๊ฐœ์”ฉ๋ฐ–์— ๋ชป ์ž…์œผ๋‹ˆ๊นŒ ๊ฒฝ์šฐ์˜ ์ˆ˜ ์กฐํ•ฉ ๋ฌธ์ œ๋กœ ์ƒ๊ฐํ•ด๋ณด๋ฉด, 

 

headgear : ์•ˆ ์ž…๋Š”๊ฒฝ์šฐ + yellowhat ์ž…๋Š” ๊ฒฝ์šฐ + green_turnban ์ž…๋Š” ๊ฒฝ์šฐ ์ด 3๊ฐœ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์žˆ๊ณ  

eyewear : ์•ˆ ์ž…๋Š” ๊ฒฝ์šฐ + bluesunglasses ์ž…๋Š” ๊ฒฝ์šฐ ์ด 2๊ฐœ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์žˆ์–ด์š”.

 

๊ทธ๋ž˜์„œ 3 x 2 = 6 ์ธ๋ฐ,

์ŠคํŒŒ์ด๋Š” ํ•˜๋ฃจ์— ์ตœ์†Œ ํ•œ ๊ฐœ์˜ ์˜์ƒ์€ ์ž…๋Š”๋‹ค๋Š” ์กฐ๊ฑด ๋•Œ๋ฌธ์— ์ „๋ถ€ ์•ˆ ์ž…๋Š” ๊ฒฝ์šฐ๋ฅผ ๋นผ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!

 

์ €๋Š” ์˜ท ๋ฐฐ์—ด์„ ๋ฐ›์ž๋งˆ์ž ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์˜ท์ข…๋ฅ˜์˜ ์ˆ˜๋ฅผ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

์ฝ”๋“œ

import Foundation

func solution(_ clothes:[[String]]) -> Int {
    var clothesKind: [String: Int] = [:]
    
    clothes.forEach {
        var count: Int = clothesKind[$0[1]] ?? 1
        count += 1
        clothesKind[$0[1]] = count
    }
    
    var result: Int = 1
    for key in clothesKind.keys {
        result *= clothesKind[key]!
    }
    return result - 1
}