Advertisement
ogv

Untitled

ogv
Sep 1st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. // Runtime: 3 ms, faster than 95.16% of Java online submissions for Find the Town Judge.
  2. // Memory Usage: 57.8 MB, less than 100.00% of Java online submissions for Find the Town Judge.
  3. class Solution {
  4.     public int findJudge(int N, int[][] trust) {
  5.         if (trust.length < N - 1) return -1;
  6.        
  7.         int[] inDegree = new int[N];
  8.         int[] outDegree = new int[N];
  9.        
  10.         for (int[] edge: trust) {
  11.             int from = edge[0] - 1;
  12.             int to = edge[1] - 1;
  13.            
  14.             outDegree[from] = outDegree[from] + 1;
  15.             inDegree[to] = inDegree[to] + 1;
  16.         }
  17.        
  18.         for (int i = 0; i < N; i++) {
  19.             if (inDegree[i] == N - 1 && outDegree[i] == 0) {
  20.                 return i + 1;
  21.             }
  22.         }
  23.        
  24.         return -1;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement