🐣 알고리즘 삐약/✏️ 냅다 덤벼보는 문제풀이

[프로그래머스] LV1. 가장 많이 받은 선뭉 | JAVA

우주수첩 2024. 8. 16. 16:50
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/258712

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

static int solution(String[] friends, String[] gifts) {
        int answer = 0;

        int num_fr = friends.length;
        Map<String, Integer> map = new HashMap<>();

        for(int i=0;i<num_fr;i++){
            map.put(friends[i],i);
        }

        int[][] presents = new int[num_fr][num_fr];
        int[] percentage = new int[num_fr];

        for(String str : gifts){
            int to = map.get(str.split(" ")[0]);
            int from = map.get(str.split(" ")[1]);

            presents[to][from] +=1;
            percentage[to]+=1;
            percentage[from]-=1;
        }


        for(int i=0;i<num_fr;i++){
            int num=0;
            for(int j=0;j<num_fr;j++){
                if(i==j) continue;

                if(presents[i][j] > presents[j][i] ||
                        (presents[i][j] == presents[j][i] && percentage[i]>percentage[j])){
                    num++;
                }
            }

            answer= Math.max(answer,num);
        }
        return answer;
    }
728x90