[알고리즘문제풀기] 의상

silver's avatar
Jan 30, 2026
[알고리즘문제풀기] 의상

문제

내가 작성한 정답

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

다른 사람들의 정답

import java.util.*; import static java.util.stream.Collectors.*; class Solution { public int solution(String[][] clothes) { return Arrays.stream(clothes) .collect(groupingBy(p -> p[1], mapping(p -> p[0], counting()))) .values() .stream() .collect(reducing(1L, (x, y) -> x * (y + 1))).intValue() - 1; } } import java.util.HashMap; import java.util.Iterator; class Solution { public int solution(String[][] clothes) { int answer = 1; HashMap<String, Integer> map = new HashMap<>(); for(int i=0; i<clothes.length; i++){ String key = clothes[i][1]; if(!map.containsKey(key)) { map.put(key, 1); } else { map.put(key, map.get(key) + 1); } } Iterator<Integer> it = map.values().iterator(); while(it.hasNext()) { answer *= it.next().intValue()+1; } return answer-1; } } import java.util.Collection; import java.util.HashMap; class Solution { public int solution(String[][] clothes) { var map = new HashMap<String, Integer>(); for (String[] strings : clothes) { int p = 0; String key = strings[1]; if(map.containsKey(key)){ p = map.get(key); } map.put(key, p+1); } Collection<Integer> values = map.values(); Integer[] counts = new Integer[values.size()]; values.toArray(counts); int[][] dp = new int[values.size()][2]; dp[0][0] = 1; dp[0][1] = counts[0]; for (int i = 1; i < dp.length; i++) { dp[i][0] = dp[i-1][0] + dp[i-1][1]; dp[i][1] = dp[i-1][0] * counts[i] + dp[i-1][1] * counts[i]; } return dp[dp.length-1][0] + dp[dp.length-1][1] -1; } }
Share article

silver