[프로그래머스 알고리즘 고득점 Kit][그래프][Java] 순위

2026. 3. 29. 01:13·알고리즘 & 자료구조/그래프

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

 

프로그래머스

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

programmers.co.kr

 

1. 정답 코드

 

뭔가 스태틱 변수를 안쓰고 메서드로 분리하려다 보니 
복잡해졌는데
내가 이긴 사람 수와 내가 진 사람 수의 합이 나를 제외한 n-1 과 같으면 순위를 알 수 있다.
그래서 2개의 단방향 그래프를 기록하고 bfs 2번을 돌리는 식으로 계산했다.
모든 노드의 거리를 알 수 있는 플로이드 워셜로도 풀 수 있다.

import java.util.*;

class Solution {
    public int solution(int n, int[][] results) {
        ArrayList<Integer>[] winWay = new ArrayList[n+1];
        ArrayList<Integer>[] loseWay = new ArrayList[n+1];
        for(int i=1; i<=n; i++){
            winWay[i] = new ArrayList<>();
            loseWay[i] = new ArrayList<>();
        }
        for(int[] result : results) {
            int w = result[0];
            int l = result[1];
            winWay[w].add(l);
            loseWay[l].add(w);
        }
        int answer = 0;
        for(int i=1; i<=n; i++){
            if(isRanked(n, i, winWay, loseWay)) answer++;
        }
        return answer;
    }
    public boolean isRanked(int n, int player, ArrayList<Integer>[] win, ArrayList<Integer>[] lose) {
        boolean[] visited = new boolean[n+1];
        Queue<Integer> q = new ArrayDeque<>();
        int winCnt = count(q, win,visited, player);
        int loseCnt = count(q, lose, visited, player);
        
        return winCnt + loseCnt == n-1;
    }
    public int count(Queue<Integer> q, ArrayList<Integer>[] result, boolean[] visited, int player) {
        int cnt = 0;
        Arrays.fill(visited, false);
        visited[player] = true;
        q.add(player);
        while(!q.isEmpty()) {
            int curr = q.poll();
            for(int next : result[curr]) {
                if(visited[next]) continue;
                
                cnt++;
                visited[next] = true;
                q.add(next);
            }
        }
        return cnt;
    }
}

 

저작자표시 비영리 변경금지 (새창열림)

'알고리즘 & 자료구조 > 그래프' 카테고리의 다른 글

[프로그래머스 알고리즘 고득점 Kit][그래프][Java] 가장 먼 노드  (0) 2026.03.07
'알고리즘 & 자료구조/그래프' 카테고리의 다른 글
  • [프로그래머스 알고리즘 고득점 Kit][그래프][Java] 가장 먼 노드
수수다
수수다
우하하
  • 수수다
    그냥살자
    수수다
  • 전체
    오늘
    어제
    • 분류 전체보기 (20) N
      • 프로젝트 (1)
      • 알고리즘 & 자료구조 (17) N
        • 분리 집합 (1)
        • 정렬 (1)
        • 유클리드 호제법 (1)
        • 이분 탐색 (2) N
        • 해시 (5)
        • 그래프 (2)
        • 스택 (1)
        • 큐 (0)
        • 완전 탐색 (1)
        • DFS (0)
        • BFS (2)
        • 힙 (1)
      • 데이터베이스 (0)
      • CS (0)
      • 기타 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 네이버 블로그
  • 공지사항

  • 인기 글

  • 태그

    귀멸의칼날
    union-find
    이분탐색
    DisjointSet
    유클리드호제법
    백엔드
    디스조인트셋
    프로그래머스
    바이브코딩
    프로그래머스 알고리즘 고득점 kit
    코딩테스트
    그래프
    코테
    알고리즘
    싸피
    코팅테스트
    깊이/너비 우선 탐색(DFS/BFS)
    bfs
    유니온파인드
    Java
    매개변수탐색
    비전공자
    SSAFY
    평균회귀
    고가용성
    분리집합
    삼성청년SW·AI아카데미
    바킹독
    해시
    완전탐색
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
수수다
[프로그래머스 알고리즘 고득점 Kit][그래프][Java] 순위
상단으로

티스토리툴바