๐Ÿ’ป Coding Problems Solving/Permutations | Combinations

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ์œ„์žฅ

Kim_dev 2022. 4. 16. 15:44

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ์œ„์žฅ

 

1. ๋ฌธ์ œ : Link

๊ฒฝ์šฐ์˜ ์ˆ˜ ์กฐํ•ฉ์„ ๋ฌป๋Š” ๋ฌธ์ œ

 

2. ํ’€์ด

ํ•ด์‰ฌ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์ด

์˜ท์„ ์•ˆ์ž…๋Š” ๊ฒฝ์šฐ๋ฅผ + 1ํ•ด์ค˜์•ผํ•˜๊ณ 

๋งˆ์ง€๋ง‰์— ์ „๋ถ€ ์•ˆ์ž…๋Š” ๊ฒฝ์šฐ๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— -1

 

3. ์ฝ”๋“œ

def solution(clothes):
    c = {}
    for clo in clothes:
        cl = clo[1]
        if cl not in c:
            c[cl] = 1
        else:
            c[cl] += 1
    answer = 1 
    for type in c: 
        answer *= (c[type] + 1)

    return answer-1
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

class Solution {
    public int solution(String[][] clothes) {
        int answer = 1; //๊ณฑ์…ˆ์„ ์œ„ํ•ด 1๋กœ ์„ ์–ธ
        HashMap<String, Integer> clothesMap = new HashMap<String, Integer>();
        //map ๊ตฌํ•˜๊ธฐ
        for(int i =0; i<clothes.length; i++){
        	//์˜์ƒ์ข…๋ฅ˜, ๊ฐฏ์ˆ˜
            clothesMap.put(clothes[i][1], clothesMap.getOrDefault(clothes[i][1], 0)+1);
        }
        //์กฐํ•ฉ
        Set<String> keySet = clothesMap.keySet(); //์˜์ƒ์ข…๋ฅ˜.
        
        for(String key : keySet) {
        	answer *= clothesMap.get(key)+1; 
        }
        return answer-1; //์•„๋ฌด๊ฒƒ๋„ ์•ˆ์ž…๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ œ๊ฑฐ
    }
}