Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 796 Bytes

leetcode_997.md

File metadata and controls

32 lines (24 loc) · 796 Bytes

LeetCode Problems

997. Find the Town Judge

class Solution {
    fun findJudge(n: Int, trust: Array<IntArray>): Int {
        val trustedCount = IntArray(n)
        // iterate
        trust.forEach { tr -> 
            // to find trusted by all, but not trust anyone, just use in/decremental calculation
            trustedCount[tr[0] - 1] --
            trustedCount[tr[1] - 1] ++
        }
        // check is there person whom trusted by all, but not trust anyone.
        for (idx in 0 until trustedCount.size) {
            if (trustedCount[idx] == n-1) return idx + 1
        }
        return -1
    }
}