-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathbest-reachable-tower.py
More file actions
40 lines (37 loc) · 1.14 KB
/
best-reachable-tower.py
File metadata and controls
40 lines (37 loc) · 1.14 KB
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
# Time: O(n)
# Space: O(1)
# array
class Solution(object):
def bestTower(self, towers, center, radius):
"""
:type towers: List[List[int]]
:type center: List[int]
:type radius: int
:rtype: List[int]
"""
best = best_x = best_y = -1
for x, y, q in towers:
if not abs(x-center[0])+abs(y-center[1]) <= radius:
continue
if q > best:
best, best_x, best_y = q, x, y
elif q == best and (x < best_x or (x == best_x and y < best_y)):
best_x, best_y = x, y
return [best_x, best_y] if best != -1 else [-1, -1]
# Time: O(n)
# Space: O(1)
# array
class Solution2(object):
def bestTower(self, towers, center, radius):
"""
:type towers: List[List[int]]
:type center: List[int]
:type radius: int
:rtype: List[int]
"""
result = [1]*3
for x, y, q in towers:
if not abs(x-center[0])+abs(y-center[1]) <= radius:
continue
result = min(result, [-q, x, y])
return result[1:] if result[0] != 1 else [-1, -1]