package com.fishercoder.solutions;

import java.util.HashSet;
import java.util.Set;

/**
 * 997. Find the Town Judge
 *
 * In a town, there are N people labelled from 1 to N.
 * There is a rumor that one of these people is secretly the town judge.
 *
 * If the town judge exists, then:
 *
 * The town judge trusts nobody.
 * Everybody (except for the town judge) trusts the town judge.
 * There is exactly one person that satisfies properties 1 and 2.
 * You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
 *
 * If the town judge exists and can be identified, return the label of the town judge.
 * Otherwise, return -1.
 *
 * Example 1:
 * Input: N = 2, trust = [[1,2]]
 * Output: 2
 *
 * Example 2:
 * Input: N = 3, trust = [[1,3],[2,3]]
 * Output: 3
 *
 * Example 3:
 * Input: N = 3, trust = [[1,3],[2,3],[3,1]]
 * Output: -1
 *
 * Example 4:
 * Input: N = 3, trust = [[1,2],[2,3]]
 * Output: -1
 *
 * Example 5:
 * Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
 * Output: 3
 *
 * Note:
 * 1 <= N <= 1000
 * trust.length <= 10000
 * trust[i] are all different
 * trust[i][0] != trust[i][1]
 * 1 <= trust[i][0], trust[i][1] <= N
 */
public class _997 {
  public static class Solution1 {
    public int findJudge(int N, int[][] trust) {
      int[] trustPoints = new int[N];
      Set<Integer> trustOthers = new HashSet<>();
      for (int[] eachTrust : trust) {
        trustPoints[eachTrust[1] - 1]++;
        trustOthers.add(eachTrust[0]);
      }
      int judge = -1;
      for (int i = 0; i < trustPoints.length; i++) {
        if (trustPoints[i] == N - 1 && !trustOthers.contains(i + 1)) {
            judge = i + 1;
        }
      }
      return judge;
    }
  }
}

@@ -82,1142 +82,8 @@
 t;%0A%0A
-/**%0A * 997. Find the Town Judge%0A *%0A * In a town, there are N people labelled from 1 to N.%0A * There is a rumor that one of these people is secretly the town judge.%0A *%0A * If the town judge exists, then:%0A *%0A * The town judge trusts nobody.%0A * Everybody (except for the town judge) trusts the town judge.%0A * There is exactly one person that satisfies properties 1 and 2.%0A * You are given trust, an array of pairs trust%5Bi%5D = %5Ba, b%5D representing that the person labelled a trusts the person labelled b.%0A *%0A * If the town judge exists and can be identified, return the label of the town judge.%0A * Otherwise, return -1.%0A *%0A * Example 1:%0A * Input: N = 2, trust = %5B%5B1,2%5D%5D%0A * Output: 2%0A *%0A * Example 2:%0A * Input: N = 3, trust = %5B%5B1,3%5D,%5B2,3%5D%5D%0A * Output: 3%0A *%0A * Example 3:%0A * Input: N = 3, trust = %5B%5B1,3%5D,%5B2,3%5D,%5B3,1%5D%5D%0A * Output: -1%0A *%0A * Example 4:%0A * Input: N = 3, trust = %5B%5B1,2%5D,%5B2,3%5D%5D%0A * Output: -1%0A *%0A * Example 5:%0A * Input: N = 4, trust = %5B%5B1,3%5D,%5B1,4%5D,%5B2,3%5D,%5B2,4%5D,%5B4,3%5D%5D%0A * Output: 3%0A *%0A * Note:%0A * 1 %3C= N %3C= 1000%0A * trust.length %3C= 10000%0A * trust%5Bi%5D are all different%0A * trust%5Bi%5D%5B0%5D != trust%5Bi%5D%5B1%5D%0A * 1 %3C= trust%5Bi%5D%5B0%5D, trust%5Bi%5D%5B1%5D %3C= N%0A */%0A
 publ
@@ -98,16 +98,18 @@
  _997 %7B%0A
+  
   public
@@ -134,24 +134,28 @@
 tion1 %7B%0A    
+  
+  
 public int f
@@ -183,24 +183,30 @@
 %5B%5D trust) %7B%0A
+      
       int%5B%5D 
@@ -237,16 +237,22 @@
 ;%0A      
+  
+    
 Set%3CInte
@@ -285,32 +285,38 @@
 hSet%3C%3E();%0A      
+     
+ 
 for (int%5B%5D eachT
@@ -335,24 +335,32 @@
 ) %7B%0A        
+    
+    
 trustPoints%5B
@@ -384,24 +384,32 @@
 ++;%0A        
+ 
+       
 trustOthers.
@@ -429,26 +429,38 @@
 %5B0%5D);%0A      
+   
-%7D%0A
+   %7D%0A      
       int ju
@@ -469,16 +469,22 @@
 e = -1;%0A
+      
       fo
@@ -536,16 +536,24 @@
         
+    
+    
 if (trus
@@ -619,16 +619,24 @@
         
+        
 judge = 
@@ -650,24 +650,32 @@
         
+       
+ 
 %7D%0A      
 %7D%0A      
@@ -666,24 +666,36 @@
 %7D%0A      
+    
-%7D%0A
+  %7D%0A   
       
+   
 return j
@@ -704,16 +704,22 @@
 ge;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 997