๐ ์ฝํ
/BOJ
[Swift] ๋ฐฑ์ค9375 ํจ์ ์ ์ ํด๋น
JerryiOS
2023. 4. 14. 05:52
import Foundation
let T = Int(readLine()!)!
for _ in 0..<T {
let n = Int(readLine()!)!
var clothes: [String: Int] = [:]
for _ in 0..<n {
let input = readLine()!.components(separatedBy: " ")
let name = input[0]
let kind = input[1]
clothes[kind, default: 0] += 1
}
let res = clothes.values.reduce(1) { $0 * ($1 + 1) } - 1
print(res)
}
๊ฐ์ ์ข ๋ฅ์ ์ท ๊ฐ์๊ฐ ๋ช๊ฐ์ธ์ง ๋ถ๋ฅํ๊ณ
๊ฐ ์ข ๋ฅ๋ณ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณฑํ๊ณ
๋ชจ๋ ์ ์ง ์์ ๊ฒฝ์ฐ์ ์๋ฅผ ๋นผ์ค๋ค.
์๋ฅผ๋ค๋ฉด
["headgear": ["hat", "turban"], "eyewear": ["sunglasses"]]
์ด๋ฐ ๊ฒฝ์ฐ
headgear์์ hat, turban, ์์ ์ ๊ฒฝ์ฐ -> (2+1)๊ฐ์ง
eyewear์์ sunglasses, ์์ ์ ๊ฒฝ์ฐ -> (1+1)๊ฐ์ง
์ด๋ฐ์์ผ๋ก ์์ ์ ๊ฒฝ์ฐ๋ ๊ณ ๋ คํ๊ธฐ ๋๋ฌธ์ ๊ณ ์ฐจํจ์์์ ($1 + 1)์ ๊ณฑํ๋๋ก ํด์ฃผ์๋ค.
๋ฐ์ํ