μμ#
μ½λ©ν μ€νΈ μ°μ΅ > ν΄μ > μμ
μ½λλ λ§€μΌ λ€λ₯Έ μ·μ μ‘°ν©νμ¬ μ λκ²μ μ’μν©λλ€.
μλ₯Ό λ€μ΄ μ½λκ° κ°μ§ μ·μ΄ μλμ κ°κ³ , μ€λ μ½λκ° λκ·Έλ μκ²½, κΈ΄ μ½νΈ, νλμ ν°μ μΈ λ₯Ό μ μλ€λ©΄ λ€μλ μ μ²λ°μ§λ₯Ό μΆκ°λ‘ μ κ±°λ λκ·Έλ μκ²½ λμ κ²μ μ κΈλΌμ€λ₯Ό μ°©μ©νκ±°λ ν΄μΌν©λλ€.
μ’ λ₯ |
μ΄λ¦ |
---|---|
μΌκ΅΄ |
λκ·Έλ μκ²½, κ²μ μ κΈλΌμ€ |
μμ |
νλμ ν°μ μΈ |
νμ |
μ²λ°μ§ |
κ²μ· |
κΈ΄ μ½νΈ |
μ½λλ κ° μ’
λ₯λ³λ‘ μ΅λ 1κ°μ§ μμλ§ μ°©μ©ν μ μμ΅λλ€. μλ₯Ό λ€μ΄ μ μμμ κ²½μ° λκ·Έλ μκ²½κ³Ό κ²μ μ κΈλΌμ€λ₯Ό λμμ μ°©μ©ν μλ μμ΅λλ€.
μ°©μ©ν μμμ μΌλΆκ° κ²ΉμΉλλΌλ, λ€λ₯Έ μμμ΄ κ²ΉμΉμ§ μκ±°λ, νΉμ μμμ μΆκ°λ‘ λ μ°©μ©ν κ²½μ°μλ μλ‘ λ€λ₯Έ λ°©λ²μΌλ‘ μ·μ μ°©μ©ν κ²μΌλ‘ κ³μ°ν©λλ€.
μ½λλ ν루μ μ΅μ ν κ°μ μμμ μ
μ΅λλ€.
μ½λκ° κ°μ§ μμλ€μ΄ λ΄κΈ΄ 2μ°¨μ λ°°μ΄ clothesκ° μ£Όμ΄μ§ λ μλ‘ λ€λ₯Έ μ·μ μ‘°ν©μ μλ₯Ό return νλλ‘ solution ν¨μλ₯Ό μμ±ν΄μ£ΌμΈμ.
μ νμ¬ν
clothesμ κ° νμ [μμμ μ΄λ¦, μμμ μ’ λ₯]λ‘ μ΄λ£¨μ΄μ Έ μμ΅λλ€.
μ½λκ° κ°μ§ μμμ μλ 1κ° μ΄μ 30κ° μ΄νμ λλ€.
κ°μ μ΄λ¦μ κ°μ§ μμμ μ‘΄μ¬νμ§ μμ΅λλ€.
clothesμ λͺ¨λ μμλ λ¬Έμμ΄λ‘ μ΄λ£¨μ΄μ Έ μμ΅λλ€.
λͺ¨λ λ¬Έμμ΄μ κΈΈμ΄λ 1 μ΄μ 20 μ΄νμΈ μμ°μμ΄κ³ μνλ²³ μλ¬Έμ λλ β_β λ‘λ§ μ΄λ£¨μ΄μ Έ μμ΅λλ€.
λ°©λ²
ν΄μν μ΄λΈ
μ²μμ μ‘°ν©μ μ΄μ©νλ κ²μΌλ‘ μκ°νλλ°
μκ³ λ³΄λ λͺ¨λ κ²½μ°μ μλ₯Ό ꡬν λ€μ λ€ μμ λ κ²½μ°λ§(-1)λ‘ λΉΌμ£Όλ©΄ λλ€.
μμ μνμ μνκ³ μΌμ€κ° μμ΄μΌ λ¬Έμ νλ νΈν κ²β¦
Python#
# input setting
problem_num = "μμ"
import os, sys
path = os.getcwd() + f"\\txt\\{problem_num}" + ".txt"
with open(path) as f:
clothes = [line.split() for line in f.read().splitlines()]
"""
solution 1
1λ²μ λͺ¨λ μ’
λ₯μ νλμ©λ§ μλ κ²½μ°κ° μκ° μ΄κ³Όκ° κ±Έλ¦Ό
collections.combinationμ μ¬μ©ν κ²½μ° μ£Όμ΄μ§ μμ μ‘°ν©μ μκ° λ§κΈ° λλ¬Έμ λͺ¨λ κ²½μ°μ μλ₯Ό ꡬνλ κ² μμ²΄κ° μλμ μΌλ‘ λΉν¨μ¨μ μ
λ°©λ²μ μλ₯Ό ꡬνλ κ²μ΄κΈ° λλ¬Έμ μ‘°ν©μ κ²½μ°λ€μ μ§μ ꡬνλ κ² λ³΄λ€ μ¬μ΄ μ κ·Όλ°©μμ΄ νμ
"""
from itertools import combinations
def solution(clothes):
# hash table λ§λ€κΈ°
closet = {}
for cloth, Type in clothes:
if Type not in closet.keys():
closet[Type] = set()
closet[Type].add(cloth)
closet[Type].add(cloth)
result = 0
for r in range(1, len(closet) + 1):
for combo in combinations(closet.keys(), r):
# νμ¬ μ‘°ν©μ λν κ°λ€μ κ³±ν΄μ λνκΈ°
product = 1
for key in combo:
product *= len(closet[key])
result += product
return result
forλ¬Έμ 3λ² λλ©΄μ combinationμ μ¬μ€μ κ³±νκΈ°λ₯Ό νλ©΄μ λνλ κ²μ΄κΈ° λλ¬Έμ μκ°ν¨μ¨μ± λ©΄μμ λ¨μ΄μ§λ κ²μΌλ‘ 보μΈλ€.
"""
solution2
λ°©λ²μ μλ§μ ꡬνλ λ°©λ².
λͺ¨λ μμ μ’
λ₯λ³λ‘ κ°κ°μ μ ννκ±°λ μ ννμ§ μλ κ²½μ°λ₯Ό λ§μ§λ§μ λΉΌμ£Όλ λ°©λ²
"""
from collections import defaultdict
def solution2(clothes):
closet = defaultdict(int)
# μμ μ’
λ₯λ³λ‘ κ°μ μΈκΈ°
for cloth, kind in clothes:
closet[kind] += 1
result = 1
for count in closet.values():
# ν΄λΉ μ’
λ₯μ μμμ μ ννμ§ μλ κ²½μ°κΉμ§ κ³ λ €νμ¬ +1
result *= count + 1
# λͺ¨λ μμμ μ ννμ§ μλ κ²½μ° νλλ₯Ό λΊ
return result - 1
solution2(clothes)
5
Javascript#
const fs = require('fs'); // Node.jsμ νμΌ μμ€ν
λͺ¨λμ λΆλ¬μ΅λλ€.
const problemNum = 'μμ'; // λ¬Έμ λ²νΈ
const path = `${__dirname}/txt/${problemNum}.txt`; // νμΌ κ²½λ‘
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
console.error('νμΌμ μ½λ λμ€ μ€λ₯κ° λ°μνμ΅λλ€.');
return;
}
// νμΌμ μ€ λ¨μλ‘ λλκ³ κ³΅λ°±μ κΈ°μ€μΌλ‘ λ°μ΄ν°λ₯Ό λΆλ¦¬ν©λλ€.
const clothes = data.split('\n').map(line => line.split(' '));
// μ΄ν clothes λ°°μ΄μ μ¬μ©νμ¬ λ°μ΄ν° μ²λ¦¬λ₯Ό μ§νν©λλ€.
console.log(clothes);
function solution(clothes) {
var closet = {};
for (var i = 0; clothes.length > i; i++) {
// var cloth = clothes[i][0];
var kind = clothes[i][1];
closet[kind] = (closet[kind] || 0) + 1;
}
var result = 1;
for (var kind in closet) {
result *= closet[kind] + 1;
}
return result - 1;
}
console.log("answer : ",solution(clothes));
});
pythonμ μμ°μ€λ¬μ΄ κ²λ€μ΄ μλ°μ€ν¬λ¦½νΈμμλ ννμ΄ μ‘°κΈμ© λ¬λΌκΈ°μ§ λλ¬Έμ
νλμ νμΌ μμμ python, javascriptλ₯Ό λμμ μ¬μ©νλ κ²μ μ΄λ €μ 보μΈλ€.