알고리즘

프로그래머스-HashMap사용 : 위장 (getOfDefault)

제이G 2022. 7. 2. 12:48

import java.util.*;
import java.util.Map.*;
class Solution {
    public int solution(String[][] clothes) {
        Map<String, Integer> map = new HashMap<>();
        for (int i=0; i<clothes.length; i++) {
            map.put(clothes[i][1], map.getOrDefault(clothes[i][1], 1) + 1);
        }
        System.out.println(map);
        
        Set<Entry<String, Integer>> set = map.entrySet();
        Iterator it = set.iterator();
        int result = 1;
        while (it.hasNext()) {
            Entry<String, Integer> entry = (Entry)it.next();
            result = result * entry.getValue();
        }
        // System.out.println(result);
        return result-1;
    }
}

 

 

'알고리즘' 카테고리의 다른 글

DFS로 조합구하기  (0) 2022.12.30
조합론 이론과 구현  (0) 2022.07.24
[프로그래머스-dfs] 타겟넘버  (0) 2022.07.02
[백준-그래프탐색-dfs] 1520 - 내리막길  (0) 2022.07.02
[백준1260] DFS와 BFS  (0) 2022.07.02