나의 개발일지

[프로그래머스] Lv. 2 의상 [Python, Java] 본문

프로그래머스

[프로그래머스] Lv. 2 의상 [Python, Java]

YoonJuHan 2023. 6. 29. 14:17
파이썬
def solution(clothes):
    answer = 1
    
    dic = {}
    
    for li in clothes:
        dic[li[1]] = dic.get(li[1], 1) + 1
    
    for key in dic:
        answer *= dic[key]
    
    return answer - 1
자바
import java.util.*;

class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
        
        Map<String, Integer> map = new HashMap<>();
        
        for (String[] arr : clothes) {
            map.put(arr[1], map.getOrDefault(arr[1], 0) + 1);
        }
        
        for (int i : map.values()) {
            answer *= i + 1;
        }

        return answer-1;
    }
}
Comments