Skip to content

Latest commit

 

History

History
50 lines (40 loc) · 1.14 KB

Question_997.md

File metadata and controls

50 lines (40 loc) · 1.14 KB

LeetCode Records - Question 997 Find the Town Judge

Attempt 1: Use a boolean[][] to save the records

class Solution {
    public int findJudge(int n, int[][] trust) {
        boolean[][] records = new boolean[n][n];

        for (int[] item : trust) {
            records[item[1] - 1][item[0] - 1] = true;
        }

        for (int i = 0; i < n; i++) {
            if (isEverybodyTrust(i, records[i]) && isTrustNobody(i, records)) {
                return i + 1;
            }
        }

        return -1;
    }

    private boolean isEverybodyTrust(int judge, boolean[] record) {
        for (int i = 0; i < record.length; i++) {
            if (i == judge) {
                continue;
            }

            if (!record[i]) {
                return false;
            }
        }

        return true;
    }

    private boolean isTrustNobody(int judge, boolean[][] records) {
        for (int i = 0; i < records.length; i++) {
            if (records[i][judge]) {
                return false;
            }
        }

        return true;
    }
}
  • Runtime: 3 ms (Beats: 60.92%)
  • Memory: 53.98 MB (Beats: 29.33%)