Skip to content

Commit 89f1261

Browse files
authored
Add nearestDrone method for drone distance calculation
Implement nearestDrone method to find the closest available drone based on Manhattan distance.
1 parent fc51128 commit 89f1261

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
3+
'''
4+
1. 아이디어 :
5+
-
6+
7+
2. 시간복잡도 :
8+
O(n)
9+
10+
3. 자료구조/알고리즘 :
11+
-
12+
13+
'''
14+
class Solution:
15+
def nearestDrone(self, drones: list[list[int]], target: list[int]) -> int:
16+
17+
def get_manhattan_distance(x1, y1, x2, y2):
18+
return abs(x1-x2) + abs(y1-y2) #|xi - xj| + |yi - yj|
19+
20+
min_distance = float('inf')
21+
ans = -1
22+
23+
for i in range(len(drones)):
24+
x, y, distance = drones[i]
25+
m_distance = get_manhattan_distance(x, y, target[0], target[1])
26+
if m_distance < min_distance and m_distance<=distance:
27+
min_distance = m_distance
28+
ans = i
29+
return ans

0 commit comments

Comments
 (0)