Published:
Updated:

  • μš°μ„ μˆœμœ„ 큐 κ³ λ €ν•˜μ§€λ„ μ•Šκ³  λ‚΄κ°€ μƒκ°ν•œ λ°©μ‹μœΌλ‘œ ν’€μ—ˆλŠ”λ° 97퍼 λ‚˜μ™”λ‹€.
    • 잘 ν’€μ—ˆλ‹€λŠ” 뜻일까..?


Solution

from typing import List


class Solution:
    def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
        answer: List[List[int]] = []

        for x, y in points:
            # μ–΄μ°¨ν”Ό 거리 λΉ„κ΅λ‹ˆκΉŒ λ£¨νŠΈλŠ” 신경쓰지 μ•Šμ•„λ„ 될 λ“―
            distance = (0 - x) ** 2 + (0 - y) ** 2
            answer.append([distance, x, y])

        answer.sort(key=lambda z: z[0])

        answer = answer[:k]

        for one in answer:
            one.pop(0)

        return answer


Reference

Leave a comment