프로그래머스 - 순위 검색

parkkhee·2023년 5월 10일
0

Level2

목록 보기
1/4
class Solution {
    static HashMap<String, ArrayList<Integer>> map;
    public int[] solution(String[] info, String[] query) {
        int[] answer = new int[query.length];

        map = new HashMap<>();

        for (String info_content : info) {

            String[] content = info_content.split(" ");
            div(content, 0, "");

        }

        int queryIdx=0;

        for (String query_content : query) {

            String str = query_content.replace(" and ", "");
            String[] content = str.split(" ");
            Collections.sort(map.get(content[0]));

            answer[queryIdx++] = binarySearch(content[0], Integer.parseInt(content[1]));
        }
        return answer;
    }

    public void div (String[] content, int idx, String str){

        if(idx==4){
            if(map.containsKey(str)) {map.get(str).add(Integer.parseInt(content[content.length - 1]));}
            else{ArrayList<Integer> arr = new ArrayList<>();
            arr.add(Integer.parseInt(content[content.length - 1]));
            map.put(str,arr);}
            return;
        }

        div(content, idx + 1, str+"-");
        div(content, idx + 1, str + content[idx]);

    }

    public int binarySearch(String key, int hope_score) {

        if (!map.containsKey(key)) {
            return 0;
        }

        ArrayList<Integer> arr = new ArrayList<>();
        arr = map.get(key);

        int start=0;
        int end = arr.size()-1;
        int mid=0;
        while (start <= end) {

            mid = (start+end)/2;

            if (hope_score > arr.get(mid)) {
                start = mid + 1;
            } else {
                end = mid-1;
            }
        }

        return arr.size() - start;

    }
}

https://school.programmers.co.kr/learn/courses/30/lessons/72412?language=java#

이진 탐색에 관한 문제였다.
완전한 답은 아니어서 ㅜ 다시 공부 해봐야 할 것 같다,,

profile
순우리말 백엔드 개발자

0개의 댓글