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