-
Notifications
You must be signed in to change notification settings - Fork 0
/
340.py
48 lines (34 loc) · 1.11 KB
/
340.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
"""
Problem:
Given a set of points (x, y) on a 2D cartesian plane, find the two closest points. For
example, given the points [(1, 1), (-1, -1), (3, 4), (6, 1), (-1, -6), (-4, -3)],
return [(-1, -1), (1, 1)].
"""
from math import sqrt
from sys import maxsize
from typing import List, Tuple
Point = Tuple[int, int]
def get_distance(pt1: Point, pt2: Point) -> float:
x1, y1 = pt1
x2, y2 = pt2
dist = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))
return dist
def get_nearest_points(pts_arr: List[Point]) -> List[Point]:
length = len(pts_arr)
dist = maxsize
pt1, pt2 = None, None
for index_1 in range(length):
for index_2 in range(index_1 + 1, length):
pt1_temp, pt2_temp = pts_arr[index_1], pts_arr[index_2]
dist_temp = get_distance(pt1_temp, pt2_temp)
if dist_temp < dist:
dist = dist_temp
pt1, pt2 = pt1_temp, pt2_temp
return [pt1, pt2]
if __name__ == "__main__":
print(get_nearest_points([(1, 1), (-1, -1), (3, 4), (6, 1), (-1, -6), (-4, -3)]))
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(1)
"""