본문 바로가기

ProgramSoliving

프로그래머스 : 순위

반응형
import java.util.*;
class Solution {
    static int digit[][];
    static final int INF = 987654321;
    public int solution(int n, int[][] results) {
        int answer = 0;
        digit = new int [n+1][n+1];
        for(int i=1;i<=n;i++)
            Arrays.fill(digit[i],INF);
        for(int[] result : results)
            digit[result[0]][result[1]] = 1;
        
        for(int k=1;k<=n;k++){
            for(int i=1;i<=n;i++){
                if(i==k)
                    continue;
                for(int j=1;j<=n;j++){
                    if(i==j)
                        continue;
                    if(digit[i][j] > digit[i][k] + digit[k][j]){
                        digit[i][j] = digit[i][k] + digit[k][j];
                    }
                }
            }
        }
        
        int parent[] = new int[n+1];
        int child[] = new int[n+1];
        
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(digit[i][j] != INF){
                    child[i]++;
                    parent[j]++;
                }
            }
        }
        
        for(int i=1;i<=n;i++)
            if(child[i] + parent[i] == n-1)
                answer++;
        
        return answer;
    }
}
반응형