🙈

⃝ 동글동글 ⃝

🪐ᐩ˖ 🍎

CodingTest/Programmers

[프로그래머스 고득점 Kit] 의상 - Hash(해시) - Lv2

JONG_UK 2023. 5. 31. 16:56
728x90
반응형
SMALL

 

⭐️ 코드 (HashSet 이용)

1. HashMap에 옷의 종류별로 개수를 카운트한다.

2. 해당 옷을 안 입는 경우도 있기 때문에 경우의 수 + 1을 해준다.

3. 모든 옷을 안 입는 경우는 없기 때문에 결과 -1을 해서 개수 세기!

import java.util.*;
class Solution {
        public int solution(String[][] clothes) {
            int answer = 0;
            HashMap<String, Integer> hashMap = new HashMap<>();
            for (int i = 0; i < clothes.length; i++) {
                hashMap.put(clothes[i][1], hashMap.getOrDefault(clothes[i][1], 0) + 1);
            }
            
            int count = 1;
            for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
                count *= entry.getValue() + 1;
            }
            answer += count - 1;
            return answer;
        }
}


💊 오답 노트

HashMap을 이용해서 같은 종류의 옷이 몇 개 있는지 카운트를 해 준 다음 계산했더니 경우의 수가 틀렸다. 뭔가 내가 푼 거지만 이상하다. 

import java.util.*;
class Solution {
        public int solution(String[][] clothes) {
            int answer = 0;
            HashMap<String, Integer> hashMap = new HashMap<>();
            for (int i = 0; i < clothes.length; i++) {
                hashMap.put(clothes[i][1], hashMap.getOrDefault(clothes[i][1], 0) + 1);
            }
            if (hashMap.size() > 1) {
                answer = 1;
                for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
                    answer *= entry.getValue();
                }
            }
            answer += clothes.length;

            return answer;
        }
}

조금 더 바꿔봤는데 비슷하다. 내가 찾지 못한 예외가 있는 게 분명하다...!

import java.util.*;
class Solution {
        public int solution(String[][] clothes) {
            int answer = 0;
            HashMap<String, Integer> hashMap = new HashMap<>();
            for (int i = 0; i < clothes.length; i++) {
                hashMap.put(clothes[i][1], hashMap.getOrDefault(clothes[i][1], 0) + 1);
            }

            int count = 1;
            for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
                int value = entry.getValue();
                count *= value;
                answer += value;
            }
            if (hashMap.size() > 1) {
                answer += count;
            }
            return answer;
        }
}

 

하나만 착용한다는 경우가 있는 것처럼 옷의 종류는 많지만 두 개만 착용하는 경우나, 세 개만 착용하는 경우 등등 있을 것이다. 그 예외를 처리해 보자. 이게 정답 코드다!! 

import java.util.*;
class Solution {
        public int solution(String[][] clothes) {
            int answer = 0;
            HashMap<String, Integer> hashMap = new HashMap<>();
            for (int i = 0; i < clothes.length; i++) {
                hashMap.put(clothes[i][1], hashMap.getOrDefault(clothes[i][1], 0) + 1);
            }
            
            int count = 1;
            for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
                count *= entry.getValue() + 1;
            }
            answer += count - 1;
            return answer;
        }
}


👀 후기

수학을 못하면 머리가 고생한다.

728x90
반응형
LIST