프로그래머스/LEVEL 2

위장

GenieLove! 2021. 4. 24. 15:30
728x90
반응형

Java

import java.util.*;
class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
        Map<String, Integer> map = new HashMap<>();//의상종류, 의상개수
        
        for(int i = 0; i < clothes.length; i++){
            map.put(clothes[i][1], map.getOrDefault(clothes[i][1], 0) + 1);
        }
        for(String key : map.keySet()){
            answer *= (map.get(key) + 1);
        }
        
        return answer - 1;
    }
}

Python

def solution(clothes):
    answer = 1
    clothesMap = dict()
    
    for i in range(len(clothes)):
        clothesMap[clothes[i][1]] = clothesMap[clothes[i][1]] + 1 if clothes[i][1] in clothesMap else 1
        
    for key in clothesMap.keys():
        answer *= (clothesMap[key] + 1)
    
    return answer - 1
728x90
반응형

'프로그래머스 > LEVEL 2' 카테고리의 다른 글

오픈채팅방  (0) 2021.05.05
짝지어 제거하기  (0) 2021.05.05
구명보트  (0) 2021.04.24
H-Index  (0) 2021.04.17
전화번호 목록  (0) 2021.04.17