Hash

폰켓몬

import java.util.*;
 
class Solution {
    public int solution(int[] nums) {
        Map<Integer, Integer> m = new HashMap<>();
        
        for (int i : nums) {
            if (m.containsKey(i)) m.put(i, m.get(i)+1);
            else m.put(i, 1);
        }
        
        if (m.size() >= nums.length/2) return nums.length/2;
        
        return m.size();
    }
}

완주하지 못한 선수

import java.util.*;

class Solution {
    public static String solution(String[] participant, String[] completion) {
        Map<String, Integer> m = new HashMap<>();
        String answer = "";
        
        for (String p : participant) {
            if (m.containsKey(p)) m.put(p, m.get(p) + 1);
            else m.put(p, 1);
        }
        
        for (String c : completion) {
            m.put(c, m.get(c) - 1);
        }
        
        for (String key : m.keySet()) {
            if (m.get(key) != 0) return key;
        }
        
        return answer;
    }
}

❌ 전화번호 목록

import java.util.HashMap;
import java.util.Map;

class Solution {
    public boolean solution(String[] phoneBook) {
        Map<String, Integer> map = new HashMap<>();

        for (int i = 0; i < phoneBook.length; i++) 
            map.put(phoneBook[i], i);
        
        for (int i = 0; i < phoneBook.length; i++)
            for (int j = 0; j < phoneBook[i].length(); j++)
                if (map.containsKey(phoneBook[i].substring(0, j)))
                    return false;

        return true;
    }
}

위장

import java.util.*;

class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
        Map<String, Integer> m2 = new HashMap<>();
        
        
        for (int i = 0; i < clothes.length; i++) {
            if (m2.containsKey(clothes[i][1])) {
                m2.put(clothes[i][1], m2.get(clothes[i][1]) + 1);
            } else {
                m2.put(clothes[i][1], 1);
            }
        }

        for (String key : m2.keySet()) {
            answer *= (m2.get(key) + 1);
        }
        
        return answer - 1;
    }
}

❌ 베스트 앨범

import java.util.*;
 
class Solution {
    public int[] solution(String[] genres, int[] plays) {
        ArrayList<Integer> answer = new ArrayList<>();
 
        HashMap<String, Integer> num = new HashMap<>(); // 장르별 총 개수
        HashMap<String, HashMap<Integer, Integer>> music = new HashMap<>(); // 장르에 속하는 노래 및 재생횟수
        for(int i = 0; i < plays.length; i++) {
            if(!num.containsKey(genres[i])) {
                HashMap<Integer, Integer> map = new HashMap<>();
                map.put(i, plays[i]);
                music.put(genres[i], map);
                num.put(genres[i], plays[i]);
            } else {
                music.get(genres[i]).put(i, plays[i]);
                num.put(genres[i], num.get(genres[i]) + plays[i]);
            }
        }
 
        List<String> keySet = new ArrayList(num.keySet());
        Collections.sort(keySet, (s1, s2) -> num.get(s2) - (num.get(s1)));
 
        for(String key : keySet) {
            HashMap<Integer, Integer> map = music.get(key);
            List<Integer> genre_key = new ArrayList(map.keySet());
 
            Collections.sort(genre_key, (s1, s2) -> map.get(s2) - (map.get(s1)));
 
            answer.add(genre_key.get(0));
            if(genre_key.size() > 1)
                answer.add(genre_key.get(1));
        }
 
        return answer.stream().mapToInt(i -> i).toArray();
    }
}

Last updated