Minimum Number of Lines to Cover Points - Problem
You are given an array points where points[i] = [xi, yi] represents a point on an X-Y plane.
Straight lines are going to be added to the X-Y plane, such that every point is covered by at least one line.
Return the minimum number of straight lines needed to cover all the points.
Input & Output
Example 1 — Basic Case
$
Input:
points = [[1,1],[2,2],[3,4],[4,5],[5,5]]
›
Output:
3
💡 Note:
One possible solution: Line 1 passes through (1,1) and (2,2); Line 2 passes through (3,4) and (4,5); Line 3 passes through (5,5). Total: 3 lines.
Example 2 — Collinear Points
$
Input:
points = [[1,1],[2,2],[3,3],[4,4]]
›
Output:
1
💡 Note:
All points are collinear (on the line y=x), so only 1 line is needed to cover all points.
Example 3 — Minimum Case
$
Input:
points = [[1,1],[2,3]]
›
Output:
1
💡 Note:
Any two points can be covered by exactly one line.
Constraints
- 1 ≤ points.length ≤ 20
- points[i].length == 2
- -100 ≤ points[i][0], points[i][1] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input Points
Given points scattered on 2D plane
2
Find Lines
Identify optimal lines that cover multiple points
3
Count Lines
Return minimum number of lines needed
Key Takeaway
🎯 Key Insight: Group collinear points together to minimize the total number of lines needed
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code