Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b2a0402

Browse files
authored
Add files via upload
1 parent 27888c7 commit b2a0402

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

‎Top_K_Elements/k_closest.py‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import heapq
2+
3+
class Point:
4+
# __init__ will be used to make a Point type object
5+
def __init__(self, x, y):
6+
self.x = x
7+
self.y = y
8+
9+
# __lt__ is used for max-heap
10+
def __lt__(self, other):
11+
return self.distance_from_origin() > other.distance_from_origin()
12+
13+
# __str__ is used to print the x and y values
14+
def __str__(self):
15+
return '[{self.x}, {self.y}]'.format(self=self)
16+
17+
# distance_from_origin calculates the distance using x, y coordinates
18+
def distance_from_origin(self):
19+
# ignoring sqrt to calculate the distance
20+
return (self.x * self.x) + (self.y * self.y)
21+
22+
__repr__ = __str__
23+
24+
25+
26+
############################################################################
27+
28+
29+
30+
def k_closest(points, k):
31+
32+
points_max_heap = []
33+
34+
# Put first k points in max_heap
35+
for i in range(k):
36+
heapq.heappush(points_max_heap, points[i])
37+
38+
# Iterate through remaining points
39+
for i in range(k, len(points)):
40+
41+
# If closer to origin, remove top point from heap and add point
42+
if points[i].distance_from_origin() < points_max_heap[0].distance_from_origin():
43+
heapq.heappop(points_max_heap)
44+
heapq.heappush(points_max_heap, points[i])
45+
46+
return list(points_max_heap)
47+
48+
49+
# Helper Function
50+
def lst_to_str(lst):
51+
out = "["
52+
for i in range(len(lst)-1):
53+
out += str(lst[i]) + ", "
54+
out += str(lst[len(lst)-1]) + "]"
55+
return out
56+
57+
58+
59+
# Time Complexity = O(nlogk)
60+
# Space Complexity = O(k)
61+
62+
63+
64+
############################################################################
65+
66+
67+
68+
# Driver code
69+
def main():
70+
points_one = [Point(1, 3), Point(3, 4), Point(2, -1)]
71+
points_two = [Point(1, 3), Point(2, 4), Point(2, -1), Point(-2, 2),
72+
Point(5, 3), Point(3, -2)]
73+
points_three = [Point(1, 3), Point(5, 3), Point(3, -2), Point(-2, 2)]
74+
points_four = [Point(2, -1), Point(-2, 2), Point(1, 3), Point(2, 4)]
75+
points_five = [Point(1, 3), Point(2, 4), Point(2, -1), Point(-2, 2),
76+
Point(5, 3), Point(3, -2), Point(5, 3), Point(3, -2)]
77+
78+
k_list = [2, 3, 1, 4, 5]
79+
points = [points_one, points_two, points_three, points_four, points_five]
80+
81+
for i in range(len(k_list)):
82+
result = k_closest(points[i], k_list[i])
83+
print(i + 1, ".\tSet of points: ", sep="", end='')
84+
print(lst_to_str(points[i]))
85+
print("\tk:", k_list[i])
86+
print("\tHere are the k =", k_list[i], "points closest to the", \
87+
"origin (0, 0): ", end='')
88+
print(lst_to_str(result))
89+
print("-"*100)
90+
91+
if __name__ == '__main__':
92+
main()
93+
94+
95+
96+
97+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /