[JAVA 문제 풀이] 331. 가장 많이 받은 선물

프로그래머스 (258712)
Stupefyee's avatar
Jun 20, 2025
[JAVA 문제 풀이] 331. 가장 많이 받은 선물
notion image
 

내가 작성한 코드

import java.util.*; class Solution { public int solution(String[] friends, String[] gifts) { int n = friends.length; // 이름을 인덱스로 매핑 Map<String, Integer> idxMap = new HashMap<>(); for (int i = 0; i < n; i++) { idxMap.put(friends[i], i); } int[][] give = new int[n][n]; // give[i][j] = i가 j에게 선물 준 횟수 int[] giveSum = new int[n]; // i가 총 준 선물 수 int[] takeSum = new int[n]; // i가 총 받은 선물 수 // 선물 기록 반영 for (String gift : gifts) { String[] arr = gift.split(" "); int from = idxMap.get(arr[0]); int to = idxMap.get(arr[1]); give[from][to]++; giveSum[from]++; takeSum[to]++; } int[] nextMonth = new int[n]; // 다음달에 받을 선물 수 // 친구 쌍 모두 비교 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { continue; } // i와 j 선물 준 횟수 비교 if (give[i][j] > give[j][i]) { nextMonth[i]++; } else if (give[i][j] == give[j][i]) { // 선물 준 횟수가 같을 경우 int scoreI = giveSum[i] - takeSum[i]; int scoreJ = giveSum[j] - takeSum[j]; if (scoreI > scoreJ) { nextMonth[i]++; } } } } // 가장 많이 받은 수 리턴 int max = 0; for (int cnt : nextMonth) { max = Math.max(max, cnt); } return max; } }
 

다른 사람의 코드

import java.util.*; class Solution { public int solution(String[] rawFriends, String[] gifts) { Friends friends = new Friends(rawFriends); friends.sendGifts(gifts); return friends.calculateMaxNextMonth(); } // 선물 배달을 위한 친구들을 관리하는 클래스 private static class Friends { private final Map<String, Friend> friends = new HashMap<>(); // 친구들을 생성하는 메서드 public Friends(String[] friends) { for (String friend : friends) { addFriend(friend); } } // 선물을 보내는 메서드 public void sendGifts(String[] gifts) { for (String gift : gifts) { String[] senderReceiver = gift.split(" "); sendGift(senderReceiver[0], senderReceiver[1]); } } // 각 친구들의 선물 카운트 증가시키는 메서드 public void sendGift(String sender, String receiver) { Friend senderFriend = friends.get(sender); Friend receiverFriend = friends.get(receiver); senderFriend.sendGift(receiverFriend); receiverFriend.receiveGift(senderFriend); } // 다음달에 선물 몇개 받는지 확인하는 메서드 public int calculateMaxNextMonth() { Map<Friend, Integer> receivedGifts = new HashMap<>(); for (Friend friend : friends.values()) { for (Friend otherFriend : friends.values()) { if (friend.isSame(otherFriend)) { continue; } if (friend.shouldReceiveFrom(otherFriend)) { receivedGifts.put(friend, receivedGifts.getOrDefault(friend, 0) + 1); } } } return receivedGifts.values().stream() .max(Integer::compareTo) .orElse(0); } // 친구를 추가하는 메서드 public void addFriend(String name) { friends.put(name, new Friend(name)); } } // 이름과 선물을 준 친구와 횟수, 선물을 받은 친구와 횟수를 저장하는 클래스 private static class Friend { private final String name; private final Map<Friend, Integer> receivedGifts = new HashMap<>(); private final Map<Friend, Integer> sentGifts = new HashMap<>(); private int giftIndex = 0; public Friend(String name) { this.name = name; } // 선물을 보내는 메서드 public void sendGift(Friend friend) { this.sentGifts.put(friend, sentGifts.getOrDefault(friend, 0) + 1); this.giftIndex++; } // 선물을 받는 메서드 public void receiveGift(Friend friend) { this.receivedGifts.put(friend, receivedGifts.getOrDefault(friend, 0) + 1); this.giftIndex--; } // 같은 이름인지 확인하는 메서드 public boolean isSame(Friend friend) { return this.name.equals(friend.name); } // 해당 친구로부터 선물을 받아야하는지 확인하는 메서드 public boolean shouldReceiveFrom(Friend friend) { int received = receivedGifts.getOrDefault(friend, 0); int sent = sentGifts.getOrDefault(friend, 0); if (received == sent) { return this.giftIndex > friend.giftIndex; } return received < sent; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Friend friend = (Friend) o; return Objects.equals(name, friend.name); } @Override public int hashCode() { return Objects.hash(name); } } }
 
Share article

stupefyee