[Programmers] 가장 많이 받은 선물 (JAVA)

2024. 10. 29. 22:57·코테/Algorithm
728x90
 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

풀이

 

중첩 HashMap을 사용해서 풀었다.

너무 어렵게 생각한 듯 하다.ㅎㅎ

결국 완탐을 돌아서 해결

 

전체코드
import java.io.*;
import java.util.*;

class Solution {
    static HashMap<String, HashMap<String, Integer>> giftsgive = new HashMap<>();
    static HashMap<String, HashMap<String, Integer>> giftsreceive = new HashMap<>();
    static HashMap<String, Integer> presentLv = new HashMap<>();
    static HashMap<String, Integer> nextMonth = new HashMap<>();
    public int solution(String[] friends, String[] gifts) {
        for(int i=0; i<friends.length; i++){
            HashMap<String, Integer> innermap = new HashMap<>();
            HashMap<String, Integer> receivemap = new HashMap<>();
            for(int j=0; j<gifts.length; j++){
                String tmp[] = gifts[j].split(" ");
                if(tmp[0].equals(friends[i])){
                    if(innermap.containsKey(tmp[1])){
                        innermap.put(tmp[1], innermap.get(tmp[1])+1);    
                    }else{
                        innermap.put(tmp[1], 1);   
                    }
                }
                if(tmp[1].equals(friends[i])){
                    if(receivemap.containsKey(tmp[0])){
                        receivemap.put(tmp[0], receivemap.get(tmp[0])+1);    
                    }else{
                        receivemap.put(tmp[0], 1);   
                    }
                }
            }
            giftsreceive.put(friends[i], receivemap);
            giftsgive.put(friends[i], innermap);
        }
        
        for(int i=0; i<friends.length; i++){
            int givecnt = 0;
            for(int j=0; j<friends.length; j++){
                if(giftsgive.get(friends[i]).containsKey(friends[j])){
                    givecnt += giftsgive.get(friends[i]).get(friends[j]);   
                }
            }
            int receivecnt = 0;
            for(int j=0; j<friends.length; j++){
                if(giftsreceive.get(friends[i]).containsKey(friends[j])){
                    receivecnt += giftsreceive.get(friends[i]).get(friends[j]);   
                }
            }
            presentLv.put(friends[i], givecnt-receivecnt);
        }
        
        for(int i=0; i<friends.length; i++){
            for(int j=0; j<friends.length; j++){
                if(i == j){
                    continue;
                }
                if(!giftsreceive.get(friends[i]).containsKey(friends[j])){
                    giftsreceive.get(friends[i]).put(friends[j], 0);
                }
                if(!giftsgive.get(friends[i]).containsKey(friends[j])){
                    giftsgive.get(friends[i]).put(friends[j], 0);
                }
            }
        }
        
        for(int i=0; i<friends.length; i++){
            
            for(int j=0; j<friends.length; j++){
                if(i == j){
                    continue;
                }
                int max = 0;
                String name = "";
                // 내가 선물을 더 많이 줬던 사람을 찾기
                if(giftsgive.get(friends[i]).containsKey(friends[j]) && giftsreceive.get(friends[i]).containsKey(friends[j])){
                    if(giftsreceive.get(friends[i]).get(friends[j]) < giftsgive.get(friends[i]).get(friends[j])){
                        if(!nextMonth.containsKey(friends[i])){
                            nextMonth.put(friends[i], 1);
                        }else{
                            nextMonth.put(friends[i], nextMonth.get(friends[i])+1);
                        }
                    }else if(giftsreceive.get(friends[i]).get(friends[j]) == giftsgive.get(friends[i]).get(friends[j])){
                        if(presentLv.get(friends[i]) > presentLv.get(friends[j])){
                            if(!nextMonth.containsKey(friends[i])){
                                nextMonth.put(friends[i], 1);
                            }else{
                                nextMonth.put(friends[i], nextMonth.get(friends[i])+1);
                            }   
                        }else{
                            continue;
                        }
                    }
                }
                // 만약 나랑 선물을 주고받지 않았던 친구가 있으면?
                if(!giftsreceive.get(friends[i]).containsKey(friends[j]) && !giftsgive.get(friends[i]).containsKey(friends[j])){
                    //선물지수를 비교해서 선물을 받을지 말지 확인해준다.
                    if(presentLv.get(friends[i]) > presentLv.get(friends[j])){
                        if(!nextMonth.containsKey(friends[i])){
                            nextMonth.put(friends[i], 1);
                        }else{
                            nextMonth.put(friends[i], nextMonth.get(friends[i])+1);
                        }   
                    }
                }
            }
        }

        int answer = 0;
        for(int i=0; i<friends.length; i++){
            if(nextMonth.containsKey(friends[i]) && answer < nextMonth.get(friends[i])){
                answer = nextMonth.get(friends[i]);
            }
        }
        return answer;
    }
}
728x90

'코테 > Algorithm' 카테고리의 다른 글

[Programmers] 미로 탈출 (JAVA)  (0) 2024.11.05
[Programmers] 신고 결과 받기 (JAVA)  (0) 2024.11.01
[Programmers] 124 나라의 숫자 (JAVA)  (0) 2024.10.28
[Programmers] PCCP 기출문제 동영상 재생기 (JAVA)  (0) 2024.10.25
[Programmers] [3차] 방금그곡 (JAVA)  (0) 2024.10.25
'코테/Algorithm' 카테고리의 다른 글
  • [Programmers] 미로 탈출 (JAVA)
  • [Programmers] 신고 결과 받기 (JAVA)
  • [Programmers] 124 나라의 숫자 (JAVA)
  • [Programmers] PCCP 기출문제 동영상 재생기 (JAVA)
DROPDEW
DROPDEW
💻 Developer | 기록하지 않으면 존재하지 않는다
  • DROPDEW
    제 2장 1막
    DROPDEW
  • 전체
    오늘
    어제
    • categories (401)
      • App/Android (1)
      • BE (36)
        • HTTP 웹 기본 지식 (8)
        • 스프링 입문 - 코드로 배우는 스프링 부트, 웹 .. (12)
        • 스프링부트와 JPA 활용 (3)
        • 스프링부트 시큐리티 & JWT (0)
        • PHP (6)
      • FE·Client (23)
        • HTML (1)
        • React (19)
        • Unity (1)
      • Data (12)
        • AI (4)
        • Bigdata (6)
        • Database (1)
        • 빅데이터분석기사 (0)
      • Infra (0)
      • CS (7)
        • CS 면접 준비 (3)
      • 취준 (13)
        • 자격증·인턴·교육 (4)
        • 인적성·NCS (6)
        • 코테·필기·면접 후기 (3)
      • 코테 (268)
        • Algorithm (220)
        • SQL (35)
        • 정리 (13)
      • 인사이트 (27)
        • 금융경제뉴스 (7)
        • 금융용어·지식 (2)
        • 북마크 (7)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    그래프탐색
    브루트포스 알고리즘
    정렬
    이분탐색
    구현
    티스토리챌린지
    오블완
    그래프이론
    매개변수탐색
    다이나믹프로그래밍
    그리디알고리즘
    문자열
    투포인터
    백준
    누적합
    너비우선탐색
    수학
    자료구조
    시뮬레이션
    최단경로
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
DROPDEW
[Programmers] 가장 많이 받은 선물 (JAVA)
상단으로

티스토리툴바