Minimum Number of Lines to Cover Points - Problem
You are given an array of points on a 2D coordinate plane, where each point is represented as
A straight line can be horizontal, vertical, or diagonal, and can extend infinitely in both directions. Multiple points can lie on the same line, which helps minimize the total number of lines needed.
Goal: Find the minimum number of lines required such that every point in the input array lies on at least one of these lines.
This problem combines geometry concepts with optimization techniques, making it perfect for testing your understanding of coordinate geometry and dynamic programming with bitmasks.
points[i] = [xi, yi]. Your task is to determine the minimum number of straight lines needed to cover all the given points, where each point must be covered by at least one line.A straight line can be horizontal, vertical, or diagonal, and can extend infinitely in both directions. Multiple points can lie on the same line, which helps minimize the total number of lines needed.
Goal: Find the minimum number of lines required such that every point in the input array lies on at least one of these lines.
This problem combines geometry concepts with optimization techniques, making it perfect for testing your understanding of coordinate geometry and dynamic programming with bitmasks.
Input & Output
example_1.py โ Basic Case
$
Input:
points = [[0,0],[1,1],[2,2],[3,4]]
โบ
Output:
2
๐ก Note:
Points [0,0], [1,1], [2,2] are collinear and can be covered by one line. Point [3,4] requires a separate line. Total: 2 lines.
example_2.py โ All Points Collinear
$
Input:
points = [[0,0],[1,1],[2,2],[3,3]]
โบ
Output:
1
๐ก Note:
All points lie on the same line y = x, so only 1 line is needed to cover all points.
example_3.py โ No Collinear Points
$
Input:
points = [[0,0],[1,2],[3,1]]
โบ
Output:
2
๐ก Note:
No three points are collinear. We need at least 2 lines: one covering two points and another covering the remaining point.
Visualization
Tap to expand
Understanding the Visualization
1
Survey Buildings
Identify all building locations (points) that need road access
2
Plan Possible Roads
Consider all possible straight roads that could connect multiple buildings
3
Optimize Road Network
Use dynamic programming to find minimum roads covering all buildings
4
Final Network
Achieve complete coverage with minimum infrastructure cost
Key Takeaway
๐ฏ Key Insight: Use bitmasks to represent which points are covered and dynamic programming to find the minimum number of lines needed to cover all points efficiently.
Time & Space Complexity
Time Complexity
O(2^n * n^2)
2^n states in bitmask DP, for each state we try O(n^2) possible lines
โ Quadratic Growth
Space Complexity
O(2^n)
Memoization table stores results for each of the 2^n possible states
โ Quadratic Space
Constraints
- 1 โค points.length โค 20
- -100 โค xi, yi โค 100
- All points are distinct
- Points can be arranged in any geometric configuration
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code