반응형
class Solution {
    public int solution(String[] friends, String[] gifts) {
        int[][] giftCount = new int[friends.length][friends.length];
        long[] giftIndex = new long[friends.length];

        //주고받은 관계 입력
        for(int i = 0 ;i< friends.length;  i++ ){
            for(int j = 0; j<friends.length; j++){
                String gift = friends[i] + " " + friends[j];
                for(String temp : gifts){
                    if(temp.equals(gift)){
                        giftCount[i][j] += 1;
                    }
                }

            }
        }

        //선물지수 입력
        for(int i=0;i< friends.length;i++){
            for(String temp : gifts) {
                String giver = temp.split(" ")[0];
                String receiver = temp.split(" ")[1];

                if (giver.equals(friends[i])) {
                    giftIndex[i] += 1;
                }

                if(receiver.equals(friends[i])){
                    giftIndex[i] -= 1;
                }
            }
        }

        int maxGift = 0;
        //선물 받을 개수 계산
        for(int i=0;i< friends.length;i++){
            int giftCnt = 0;
            for(int j =0;j< friends.length;j++){
                //자기거는 제외
                if(i==j){
                    continue;
                }
                
                //주고받은 내역 없는 경우
                if(giftCount[i][j] == giftCount[j][i]){
                    if(giftIndex[i] > giftIndex[j]){
                        giftCnt += 1;
                    }
                    
                }else{
                    //비교
                    if(giftCount[i][j] > giftCount[j][i]){
                        giftCnt += 1;
                    }
                }
                

            }
            
            if(giftCnt>maxGift) {
                maxGift = giftCnt;
            }
        }
        
        return maxGift;
    }
    
}
반응형

+ Recent posts