973. K Closest Points to Origin
https://youtu.be/rI2EBUEMfTk?si=rQzq5oqyjL2M1qgb
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
# k번째 가까운 points 리턴,
# [distance, x, y] 로 저장
# minHeap 사용
minHeap = []
for x, y in points:
dist = (x**2) + (y**2)
minHeap.append([dist, x, y])
heapq.heapify(minHeap)
res = []
while k > 0:
dist, x, y = heapq.heappop(minHeap)
res.append([x, y])
k -=1
return res
'LeetCode > 주제별 보충' 카테고리의 다른 글
Heap-PrioiryQueue: 621. Task Scheduler ★★★ (0) | 2025.01.20 |
---|---|
Heap-PrioiryQueue: 215. Kth Largest Element in an Array ★ (0) | 2025.01.20 |
Heap-PrioiryQueue: 1046. Last Stone Weight (0) | 2025.01.20 |
Heap-PrioiryQueue: 703. Kth Largest Element in a Stream ★ (0) | 2025.01.20 |
Backtracking: 51. N-Queens (0) | 2025.01.19 |