Statement▼
Given a 2D integer array, points
, where points[i]
Note: Straight lines will be added to the XY plane to ensure that every point is covered by at least one line.
Constraints:
1≤ points.length
≤10 points[i].length
==2 −100≤ xi,yi ≤100 All the points are unique.
Solution
In this solution, we are solving a computational math and geometry problem of finding the minimum number of straight lines required to cover all given points in an XY plane. Let’s break down the logic, incorporating key mathematical reasoning and geometric approaches.
Slope and y-intercept
Two parameters can uniquely identify a straight line:
Slope: Determines the line’s steepness and direction, indicating how much the line rises or falls for a given horizontal change.
y-intercept: Specifies where the line crosses the y-axis.
For two points,
The y-intercept,
Note: These above two parameters uniquely define a line on the XY plane. For vertical lines
(x1=x2) , theslope is undefined, and we handle such cases separately by usingslope =x1 withy-intercept =∞ .
Using the slope to identify collinear points:
A single line can pass through multiple points if they are collinear (on the same line). Or, we can say that two or more points are collinear if they share the same slope when paired. Calculating the slopes between points allows us to group points on the same line. As collinear points share the same slope and y-intercept, the problem reduces to finding sets of points with identical slope and y-intercept values.
This grouping is key to identifying and storing the lines in the solution.
Handling lines with more than two points:
Initially, we know at least
For lines covering more than
Subset representation:
For
Each subset is represented by a binary number of
m bits.A bit value of
1 means the corresponding line is included in the subset, while0 means it is excluded.
For example, with
01 : Includes only the1st line.10 : Includes only the2nd line.11 : Includes both lines.
Processing subsets using bit masking:
We track the points covered by the selected lines in a subset. For each subset:
We iterate through each bit in its binary representation and evaluate its coverage by checking if the current bit is 1. If it is, we store all points covered by that line to the tracking set of currently covered points. Otherwise, we skip that line.
Next, we calculate uncovered points by subtracting the number of covered points in the tracking set from the total number of points,
n .The total number of lines required is calculated as follows:
The final result is the minimum of
Here’s the step-by-step implementation of the solution:
Initialize a dictionary,
line_points
, to store the unique points on a respective line.Iterate through all the pairs of points and for each current point
(x
1
, y
1
)
:Fix
(x
1
, y
1
)
and iterate for all the other remaining points(x
2
, y
2
)
, calculateslope
andy_intercept
of pairs(x
1
, y
1
)
and(x
2
, y
2
)
.If
x
1
== x
2
, meaning both points lie vertically:The slope becomes
x1
, and the intercept will be∞ because vertical lines have an undefined slope.
Otherwise, calculate
slope
andy_intercept
.Add these values to
line_points
withslope
andy-intercept
as the key and the respective points as the value.
Initialize an array,
lines
, to store theslope
andy_intercept
of the lines having more than2 points.Initially, initialize the answer with the
⌈n/2⌉ because minimum⌈n/2⌉ lines can cover any number of points.Initialize
m
with the length of the lines having more than2 points.Now, initialize a
mask
for all the subsets of them
, which are2
m
:Initialize
count_of_1s
with the number of in the mask.Initialize a set,
current_points
, to store the current subset of the line. Also, initialize a variablej
to0 , to keep track of the current bit.While the
mask
is not0 :If the last bit (rightmost bit) of the
mask
is1 :Add the points covered by the line to
current_points
.
Shift the bits of
mask
one position to the right.Increment
j
to point to the next bit.
Calculate the
uncovered_points
by subtracting thecurrent_points
from total pointsn
.Update the
answer
by calculating themin(answer, count_of_1s + ⌈uncovered_points/2)⌉)
.
Return
answer
, which now represents the minimum number of lines to cover all points.
Let’s look at the following illustration to get a better understanding of the solution: