μ˜μƒ

Contents

μ˜μƒ#

  • μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ > ν•΄μ‹œ > μ˜μƒ

μ½”λ‹ˆλŠ” 맀일 λ‹€λ₯Έ μ˜·μ„ μ‘°ν•©ν•˜μ—¬ μž…λŠ”κ²ƒμ„ μ’‹μ•„ν•©λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ μ½”λ‹ˆκ°€ 가진 옷이 μ•„λž˜μ™€ κ°™κ³ , 였늘 μ½”λ‹ˆκ°€ λ™κ·Έλž€ μ•ˆκ²½, κΈ΄ μ½”νŠΈ, νŒŒλž€μƒ‰ ν‹°μ…”μΈ λ₯Ό μž…μ—ˆλ‹€λ©΄ λ‹€μŒλ‚ μ€ 청바지λ₯Ό μΆ”κ°€λ‘œ μž…κ±°λ‚˜ λ™κ·Έλž€ μ•ˆκ²½ λŒ€μ‹  κ²€μ • μ„ κΈ€λΌμŠ€λ₯Ό μ°©μš©ν•˜κ±°λ‚˜ ν•΄μ•Όν•©λ‹ˆλ‹€.

μ’…λ₯˜

이름

μ–Όκ΅΄

λ™κ·Έλž€ μ•ˆκ²½, κ²€μ • μ„ κΈ€λΌμŠ€

μƒμ˜

νŒŒλž€μƒ‰ ν‹°μ…”μΈ 

ν•˜μ˜

청바지

κ²‰μ˜·

κΈ΄ μ½”νŠΈ

μ½”λ‹ˆλŠ” 각 μ’…λ₯˜λ³„λ‘œ μ΅œλŒ€ 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λ₯Ό λ™μ‹œμ— μ‚¬μš©ν•˜λŠ” 것은 μ–΄λ €μ›Œ 보인닀.