Zero to Hero
article thumbnail
 

Queries on Number of Points Inside a Circle - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

원의 중심 좌표와 반지름의 길이가 주어지고, N개의 점이 입력으로 주어진다.

해당 원 안에 포함되거나 걸치는 점의 개수를 각각의 원에 대해서 반환하는 문제다.

 

from typing import *


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

        for query in queries:
            cx, cy, length = query
            temp = 0
            for point in points:
                x, y = point
                if (cx - x) * (cx - x) + (cy - y) * (cy - y) <= length * length:
                    temp += 1
            answer.append(temp)

        return answer

문제는 간단한데, 원의 중심과 주어진 점의 거리가 원의 반지름보다 같거나 작은 경우에 카운트를 올려주면 된다.

 

하지만 이 문제는 굉장히 재밌는 문제인데 관련한 내용은 아래 포스트 참고.

 

N*N, N**2, pow(N, 2), math.pow(N,2)

이전에 재밌는 문제를 풀었다. 관련 내용은 아래 포스팅 참고. 1828. Queries on Number of Points Inside a Circle Queries on Number of Points Inside a Circle - LeetCode Level up your coding skills and qu..

doljae.tistory.com

 

profile

Zero to Hero

@Doljae

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!