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
Minimum Lines to Cover Points Problem(1,1)(2,2)(3,3)(4,4)(5,5)Line 1: y = xLine 2Points (1,1), (2,2), (3,3), (4,4) are collinear → 1 linePoint (5,5) needs separate line → Total: 2 linesGoal: Find minimum number of lines to cover all points
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
Asked in
Google 15 Meta 12 Amazon 8
12.5K Views
Medium Frequency
~35 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen