← 返回 snapchat 的题目列表Find the Spy
类型:online_judge
Find the Spy in the organization. Assume in an organization, everyone accuses someone else of being a spy with a trust relationship, and only one person is accused by the majority. Find the person who is accused by the majority, i.e., the spy. Given a trust relationship graph of n people, where trust[i] is a two-element array [a, b] representing a accuses b of being a spy. Solve the problem in O(n) time. Example Input/Output:
Input: n = 3, trust = [[1, 3], [2, 3]] Output: 3
Input: n = 3, trust = [[1, 3], [2, 3], [3, 1]] Output: -1 (no spy)
Constraints: 1 <= n <= 1000 1 <= trust.length <= 10^4
Example
Input
3
[[1, 3], [2, 3]]