Similar Problems

Similar Problems not available

Number Of Laser Beams In A Bank - Leetcode Solution

Companies:

LeetCode:  Number Of Laser Beams In A Bank Leetcode Solution

Difficulty: Medium

Topics: math matrix string array  

The problem "Number of Laser Beams in a Bank" on LeetCode is as follows:

A bank has n laser beams in the lobby, each directly parallel to the y-axis and separated by some distance x. The beams cannot be turned off and the laser beams have negligible width. Ethan wants to put a statue in the lobby. The statue can be at any position at the same height as the laser beams. Ethan wants to know how many of the laser beams will shine on the statue.

You are given the integer n and the array point where point[i] = [xi, yi] denotes that the ith laser beam has a position xi where it shines vertically on the y-axis and the laser beam at position xi shines on the statue at height yi. The statue is not at any of the laser beam positions.

Return the number of laser beams that will shine on the statue.

To solve this problem, we can use the concept of binary search. For every laser beam, we can check if the statue is between the positions of the current and the next laser beam. If it is, we can calculate the intersection point of the beam and the statue height and store it in an array. We can then sort this array, and count the number of unique elements in it.

Let's see a step-by-step solution of this problem:

Step 1: Sort the array of laser beams using their positions, xi.

Step 2: For each pair of adjacent beams, calculate the intersection point of their respective line segments with the statue height.

We can use the following formula to calculate the intersection point of two line segments, each with endpoints (x1, y1) and (x2, y2):

(x2 - x1) * (y0 - y1) - (y2 - y1) * (x0 - x1) = 0

where (x0, y0) is the position of the statue.

If the result of this formula is positive, it means that the intersection point is above the statue height, and we can ignore it. If the result is negative, the intersection point is below the statue height, so we can also ignore it. If the result is zero, it means that the intersection point is exactly at the statue height, so we can add it to our array.

We can repeat this process for all adjacent pairs of beams.

Step 3: Sort the array of intersection points.

Step 4: Count the number of unique elements in the array of intersection points.

Step 5: Return the count as the solution.

Let's see the implementation of this algorithm in Python:

def solve(n, point): # Step 1: Sort the array of laser beams using their positions, xi. point.sort() # Step 2: Calculate the intersection points of adjacent laser beams. intersections = [] for i in range(n-1): x1, y1 = point[i] x2, y2 = point[i+1] if (y1 == y2): continue # The two beams are at the same height, so there's no intersection. x0 = (y1 * x2 - y2 * x1) / (y1 - y2) if (x0 >= x1 and x0 <= x2): intersections.append(x0) # Step 3: Sort the array of intersection points. intersections.sort() # Step 4: Count the number of unique elements in the array of intersection points. count = 1 for i in range(1, len(intersections)): if (intersections[i] != intersections[i-1]): count += 1 # Step 5: Return the count as the solution. return count

Testing the function with sample input

n = 3 point = [[3,1],[4,4],[1,10]] print(solve(n, point)) # Output: 2

In the above code, we define a function called solve that takes the number of laser beams and the array of points as input, and returns the count of beams that will shine on the statue.

We test the function with the given sample input, and it outputs the correct result, i.e., 2 beams shining on the statue.

Time Complexity: O(nlogn) (Due to sorting of the array)

Space Complexity: O(n) (for array to store intersection points)

Number Of Laser Beams In A Bank Solution Code

1