In a town, there are
N
people labelled from1
toN
. 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 pairstrust[i] = [a, b]
representing that the person labelleda
trusts the person labelledb
.
If the town judge exists and can be identified, return the label of the town judge. Otherwise, return
-1
.
Note:
1 <= N <= 1000
trust.length <= 10000
trust[i]
are all differenttrust[i][0] != trust[i][1]
1 <= trust[i][0], trust[i][1] <= N
在一個小鎮中,有
N
個人分別被標記上1
到N
。有一個傳言說這群人中有一個秘密小鎮法官。
如果小鎮法官存在,那麼:
- 小鎮法官不信任任何人。
- 所有人(除了小鎮法官)相信小鎮法官。
- 剛好有一個人符合第一條和第二條。
給你
trust
,一個對子的陣列trust[i] = [a, b]
代表某個被標記為a
的人相信某個被標記為b
的人。
如果小鎮法官存在且能夠被證明,回傳小鎮法官的標記。否則回傳
-1
。
提示:
1 <= N <= 1000
trust.length <= 10000
- 全部的
trust[i]
都不同trust[i][0] != trust[i][1]
1 <= trust[i][0], trust[i][1] <= N
0
,指入為N - 1
,並且為唯一的人。in
最多就是N - 1
。out
,只要當i
發生指出的時候,破壞掉in[i] == N - 1
的可能就好了。in[i]--
,如此就可以省下一個陣列變數的空間。LeetCode
C++