[프로그래머스 알고리즘 고득점 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;
    }
}

 

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

'알고리즘 & 자료구조 > 문제 풀이' 카테고리의 다른 글

[LeetCode][Java] 1768. Merge Strings Alternately  (0) 2026.04.16
[프로그래머스 알고리즘 고득점 Kit][이분탐색][Java] 징검다리  (0) 2026.03.31
[프로그래머스 알고리즘 고득점 Kit][힙(Heap)][Java] 더 맵게  (0) 2026.03.29
[프로그래머스 알고리즘 고득점 Kit][깊이/너비 우선 탐색(DFS/BFS)][Java] 네트워크  (0) 2026.03.08
[프로그래머스 알고리즘 고득점 Kit][그래프][Java] 가장 먼 노드  (0) 2026.03.07
'알고리즘 & 자료구조/문제 풀이' 카테고리의 다른 글
  • [LeetCode][Java] 1768. Merge Strings Alternately
  • [프로그래머스 알고리즘 고득점 Kit][이분탐색][Java] 징검다리
  • [프로그래머스 알고리즘 고득점 Kit][힙(Heap)][Java] 더 맵게
  • [프로그래머스 알고리즘 고득점 Kit][깊이/너비 우선 탐색(DFS/BFS)][Java] 네트워크
수수다
수수다
우하하
  • 수수다
    그냥살자
    수수다
  • 전체
    오늘
    어제
    • 분류 전체보기 (37) N
      • 프로젝트 (1)
      • 알고리즘 & 자료구조 (18) N
        • 내용 정리 (2)
        • 문제 풀이 (16) N
      • 데이터베이스 (14) N
        • 내용 정리 (1) N
        • 문제 풀이 (13) N
      • CS (2)
      • 기타 (2)
  • 블로그 메뉴

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

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

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바