Skip to content

Commit db20388

Browse files
author
Partho Biswas
committed
997_Find_the_Town_Judge
1 parent 67a48cb commit db20388

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
541541
|32| **[909. Snakes and Ladders](https://tinyurl.com/yazkm38f)** | [Python](https://tinyurl.com/wu6rdaw/909_Snakes_and_Ladders.py), [Swift](https://tinyurl.com/wuja3c4/909_Snakes_and_Ladders.swift) | [Art 1](https://tinyurl.com/ya45spcc), [Art 2](https://tinyurl.com/ycafdfr4), [Art 3](https://tinyurl.com/yc3hdaw9), [Vid 1](https://tinyurl.com/yc3hdaw9), [Vid 2](https://tinyurl.com/y99hrfvk), | Medium |📌 BFS. Check again |
542542
|33| **[1192. Critical Connections in a Network](https://tinyurl.com/y6u2p7qw)** | [Python](https://tinyurl.com/wu6rdaw/1192_Critical_Connections_in_a_Network.py), [Swift](https://tinyurl.com/wuja3c4/1192_Critical_Connections_in_a_Network.swift) | [Art 1](https://tinyurl.com/yc2tzb4k), [Art 2](https://tinyurl.com/ycjd3rrr), [Art 3](https://tinyurl.com/ybj5nux8), [Art 4](https://tinyurl.com/y9wz5w8o), [Vid 1](https://tinyurl.com/y76cux54), [Vid 2](https://tinyurl.com/ybmgmv5l), [Vid 3](https://tinyurl.com/y7sa5oou) | Hard |📌 **Important, Learned new things.Tarjans SCCs algorithm. Check again** |
543543
|34| **[694. Number of Distinct Islands](https://tinyurl.com/ybf8cedn)** | [Python](https://tinyurl.com/wu6rdaw/694_Number_of_Distinct_Islands.py), [Swift](https://tinyurl.com/wuja3c4/694_Number_of_Distinct_Islands.swift) | [Art 1](https://tinyurl.com/y7rapyk6), [Art 2](https://tinyurl.com/yde8dqbb) | Medium |📌 |
544+
|35| **[997. Find the Town Judge](https://tinyurl.com/y7875kls)** | [Python](https://tinyurl.com/wu6rdaw/997_Find_the_Town_Judge.py), [Swift](https://tinyurl.com/wuja3c4/997_Find_the_Town_Judge.swift) | --- | Easy |📌 |
544545

545546

546547
</p>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from collections import defaultdict
2+
3+
class Solution(object):
4+
def findJudge(self, N, trust):
5+
"""
6+
:type N: int
7+
:type trust: List[List[int]]
8+
:rtype: int
9+
"""
10+
11+
graph = defaultdict(list)
12+
13+
for i in range(1, N + 1):
14+
graph[i] = []
15+
16+
for trustPair in trust:
17+
parent, child = trustPair
18+
graph[parent].append(child)
19+
20+
judges = []
21+
# Checks for first condition. If someone doesn't trust anyone then he/she is a probable candidate for town judge
22+
for i in range(1, N + 1):
23+
if not graph[i] or len(graph[i]) == 0:
24+
judges.append(i)
25+
26+
# Violets second conditions. Because there is someone else or noone that doesn't truse the town judge
27+
if len(judges) != 1:
28+
return - 1
29+
30+
# Checking for third condition
31+
for i in range(1, N + 1):
32+
childSet = set(graph[i])
33+
if judges[-1] != i and judges[-1] not in childSet:
34+
return -1
35+
36+
return judges[-1]
37+
38+
39+
"""
40+
outDegree/edgeList is 0
41+
42+
"""

0 commit comments

Comments
 (0)