코딩테스트(Java)/프로그래머스 1단계

[프로그래머스] 가장 많이 받은 선물 - JAVA

영드로이드개발자 2024. 2. 27. 13:43
반응형

문제

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

 

프로그래머스

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

programmers.co.kr

입출력

문제풀이

이 문제는 카카오 2024 겨울 인턴십에서 나온 문제이다.

단순한 구현 문제였기 때문에 문제의 조건만 잘 이해하고 문제를 풀면 어렵지는 않게 문제를 풀 수 있는 문제이다.

HashMap과 배열등을 통해 문제를 풀어보았는데 썩 좋지 못한 방법인것 같은 느낌이 든다..

더 좋은 아이디어와 해결 방법이 있으면 공유 부탁 드려요.

 

import java.util.*;


class Solution {

    static int[][] arr;
    static boolean[][] check;
    static int[] countGift; // 다음달에 선물 받을 갯수

    public static void main(String[] args) {
        String[] friends = {"joy", "brad", "alessandro", "conan", "david"};
        String[] gifts = {"alessandro brad", "alessandro joy", "alessandro conan", "david alessandro", "alessandro david"}; // 이번달에 선물을 준 수
        System.out.println(solution(friends, gifts));
    }

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

        arr = new int[friends.length][friends.length];
        check = new boolean[friends.length][friends.length];
        countGift = new int[friends.length];

        HashMap<String, Integer> dataMap = new HashMap<>();

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

        for (String gift : gifts) {
            StringTokenizer st = new StringTokenizer(gift);
            String from = st.nextToken();
            String to = st.nextToken();
            arr[dataMap.get(from)][dataMap.get(to)]++;
        }


        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (i == j) continue; // 자기 자신한테는 선물을 주지 못함
                if (check[i][j] || check[j][i]) continue; // 중복확인 제거
                if (arr[i][j] > arr[j][i]) {
                    check[i][j] = true;
                    countGift[i]++;
                } else if (arr[i][j] < arr[j][i]) {
                    check[i][j] = true;
                    countGift[j]++;
                } else if ((arr[i][j] == 0 && arr[j][i] == 0) || arr[i][j] == arr[j][i]) {
                    check[i][j] = true;
                    int giftScoreA, giftScoreB; // 선물 지수 = 자신이 준 선물 - 자신이 받은 선물
                    int fromA = 0; // 자신이 받은 선물
                    int fromB = 0;
                    int toA = 0; // 자신이 준 선물
                    int toB = 0;

                    // 자신이 받은 선물들과 자신이 준 선물들의 합을 구함
                    for (int k = 0; k < arr.length; k++) {
                        toA += arr[i][k];
                        toB += arr[j][k];
                        fromA += arr[k][i];
                        fromB += arr[k][j];
                    }

                    // 각각의 선물지수
                    giftScoreA = toA - fromA;
                    giftScoreB = toB - fromB;

                    if (giftScoreA > giftScoreB) {
                        countGift[i]++;
                    } else if (giftScoreB > giftScoreA) {
                        countGift[j]++;
                    }
                }
            }
        }

        for (int i = 0; i < countGift.length; i++){
            if (answer < countGift[i]) answer = countGift[i];
        }

        return answer;
    }

}

느낀점

문제의 내용만 잘 이해하면 크게 어렵지 않게 구현할 수 있었던 문제였다.

반응형