소프트웨어에 대한 모든 것

LeetCode 풀이 - 1828. Queries on Number of Points Inside a Circle 본문

알고리즘/LeetCode

LeetCode 풀이 - 1828. Queries on Number of Points Inside a Circle

앤테바 2021. 10. 16. 17:02
반응형

Queries on Number of Points Inside a Circle

문제)

풀이 아이디어)

  • 원안에 점(points)이 몇 개 있는가?
  • 원의 중심과 점과의 거리가 r 보다 작다면 원안에 있는 것을 의미

솔루션1)

class Solution:
    def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
        ret = []
        for circle in queries:
            count_in_circle = 0
            for point in points:
                distance = self._distance(circle, point)
                if distance <= circle[2]:
                    count_in_circle += 1
            ret.append(count_in_circle)
        return ret
            
            
    def _distance(self, circle, point):
        return sqrt(pow(circle[0] - point[0], 2) + pow(circle[1] - point[1], 2))

솔루션2)

class Solution:
    def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:        
        return [sum(self._distance_hypot(circle, point) <= circle[2] for point in points) for circle in queries]
            
    def _distance(self, circle, point):
        return sqrt(pow(circle[0] - point[0], 2) + pow(circle[1] - point[1], 2))
반응형
Comments