Skip to content

Commit 3050683

Browse files
authored
Update Find the Celebrity.py
1 parent 3552976 commit 3050683

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

Find the Celebrity.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,19 @@ def findCelebrity(self, n):
2020
:type n: int
2121
:rtype: int
2222
"""
23-
dic = {}
23+
candidate = 0
24+
for i in xrange(1, n):
25+
if knows(candidate, i):
26+
candidate = i
27+
28+
for i in xrange(candidate):
29+
if knows(candidate, i):
30+
return -1
31+
2432
for i in xrange(n):
25-
# test if label i is celebrity
26-
is_celebrity = True
27-
for j in xrange(n):
28-
if i == j:
29-
continue
30-
if self.knows(j, i, dic) and not self.knows(i, j, dic):
31-
pass
32-
else:
33-
is_celebrity = False
34-
break
35-
if is_celebrity:
36-
return i
37-
38-
return -1
39-
40-
def knows(self, a, b, dic):
41-
if (a, b) not in dic:
42-
dic[(a, b)] = knows(a, b)
43-
return dic[(a, b)]
33+
if i == candidate:
34+
continue
35+
if not knows(i, candidate):
36+
return -1
37+
38+
return candidate

0 commit comments

Comments
 (0)