Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Number of Darts Inside of a Circular Dartboard

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1453" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

Alice is throwing n darts on a very large wall. You are given an array darts where darts[i] = [xi, yi] is the position of the ith dart that Alice threw on the wall. Bob knows the positions of the n darts on the wall. He wants to place a dartboard of radius r on the wall so that the maximum number of darts that Alice throws lie on the dartboard. Given the integer r, return the maximum number of darts that can lie on the dartboard. Example 1: Input: darts = [[-2,0],[2,0],[0,2],[0,-2]], r = 2 Output: 4 Explanation: Circle dartboard with center in (0,0) and radius = 2 contain all points. Example 2: Input: darts = [[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]], r = 5 Output: 5 Explanation: Circle dartboard with center in (0,4) and radius = 5 contain all points except the point (7,8). Constraints: 1 <= darts.length <= 100 darts[i].length == 2 -104 <= xi, yi <= 104 All the darts are unique 1 <= r <= 5000

Explanation

Here's an efficient solution to the dartboard problem:

  • Iterate Through All Possible Centers: The core idea is to iterate through each dart as a potential center of the dartboard. Then, consider points within a certain range from that dart and treat those locations as potential circle centers.
  • Check Points Within Radius: For each potential center, we check how many darts fall within the given radius r.
  • Maximize Count: Keep track of the maximum number of darts found within the radius across all possible centers.

  • Runtime Complexity: O(n3) where n is the number of darts.

  • Storage Complexity: O(n) in worst case.
import math

def solve():
    darts = [[-2,0],[2,0],[0,2],[0,-2]]
    r = 2
    print(max_darts_inside(darts, r))

    darts = [[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]]
    r = 5
    print(max_darts_inside(darts, r))

def max_darts_inside(darts, r):
    n = len(darts)
    max_count = 0

    for i in range(n):
        for j in range(n):
            #Consider mid point between 2 darts as the possible circle center
            x1, y1 = darts[i]
            x2, y2 = darts[j]

            mid_x = (x1 + x2) / 2.0
            mid_y = (y1 + y2) / 2.0

            #Checking if the distance between mid point and either dart is <= r
            distance = math.sqrt((mid_x - x1)**2 + (mid_y - y1)**2)
            if distance <= r:
                 count = 0
                 for k in range(n):
                    x, y = darts[k]
                    dist = math.sqrt((mid_x - x)**2 + (mid_y - y)**2)
                    if dist <= r:
                        count += 1
                 max_count = max(max_count, count)

        #Consider a dart as the center

        x1, y1 = darts[i]
        count = 0
        for k in range(n):
            x, y = darts[k]
            dist = math.sqrt((x1 - x)**2 + (y1 - y)**2)
            if dist <= r:
                count += 1
        max_count = max(max_count, count)

    return max_count


    # Code
    ```python
    import math
def max_darts_inside(darts, r):
    n = len(darts)
    max_count = 0

    for i in range(n):
        for j in range(n):
            #Consider mid point between 2 darts as the possible circle center
            x1, y1 = darts[i]
            x2, y2 = darts[j]

            mid_x = (x1 + x2) / 2.0
            mid_y = (y1 + y2) / 2.0

            #Checking if the distance between mid point and either dart is <= r
            distance = math.sqrt((mid_x - x1)**2 + (mid_y - y1)**2)
            if distance <= r:
                 count = 0
                 for k in range(n):
                    x, y = darts[k]
                    dist = math.sqrt((mid_x - x)**2 + (mid_y - y)**2)
                    if dist <= r:
                        count += 1
                 max_count = max(max_count, count)

        #Consider a dart as the center

        x1, y1 = darts[i]
        count = 0
        for k in range(n):
            x, y = darts[k]
            dist = math.sqrt((x1 - x)**2 + (y1 - y)**2)
            if dist <= r:
                count += 1
        max_count = max(max_count, count)

    return max_count

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Maximum Number of Darts Inside of a Circular Dartboard