-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path356 Line Reflection.py
51 lines (40 loc) · 1 KB
/
356 Line Reflection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Premium Question
Author: Rajeev Ranjan
"""
from collections import defaultdict
class Solution(object):
def __init__(self):
self.x = None
def isReflected(self, points):
"""
:type points: List[List[int]]
:rtype: bool
"""
d = defaultdict(list)
for x, y in points:
d[y].append(x)
for v in d.values():
if not self.check(v):
return False
return True
def check(self, lst):
lst.sort()
i = 0
j = len(lst) - 1
while i < j:
x = (lst[i] + lst[j]) / float(2)
if not self.x:
self.x = x
elif self.x != x:
return False
i += 1
j -= 1
if i == j:
if not self.x:
self.x = lst[i]
elif self.x != lst[i]:
return False
return True
if __name__ == "__main__":
assert Solution().isReflected([[1,1],[-1,-1]]) == False