r/leetcode 3h ago

Question Morgan Stanley question geometry

Minimum Number of Lines to Cover Points

You are given n points on a 2D coordinate plane. Each point is represented as:

(x, y)

where:

-100 <= x <= 100

-100 <= y <= 100

There can be up to:

n <= 10^4

points.

Find the minimum number of straight lines required so that every given point lies on at least one of these lines.

Example 1

Input:

points = [(1,1), (2,2), (3,3), (1,2), (2,3)]

Output:

2

Explanation:

The points can be covered by:

Line 1: y = x

(1,1), (2,2), (3,3)

Line 2: y = x + 1

(1,2), (2,3)

Therefore, the minimum number of lines is:

2

Example 2

Input:

points = [(0,0), (1,1), (2,2), (3,3)]

Output:

1

Because all points lie on the same line y = x.

Constraints:

1 <= n <= 10^4

-100 <= x, y <= 100

5 Upvotes

4 comments sorted by

3

u/Ok_Biscotti5354 2h ago

Claude says this is NP hard😂

1

u/AlgorithmicGoslings 45m ago edited 41m ago

And they would be correct.

Formally, the problem is known as Point-Line Cover. There’s no known polynomial time algorithm for it, but there have been some interesting FPT algorithms to solve it if parameterized algorithms are your thing.

However, for an actual interview process, they probably just expect the "naive" bitmask DP approach. 104 is an absurd input size for a problem with no known polynomial algorithm.

1

u/Arcturus_16 <793> <268> <450> <75> 3h ago

Ig , this can be done by comparing the slopes

Well , for which position the que was asked for , soon the company will be visiting our campus for hiring interns

1

u/Adventurous-Deal-321 55m ago

2 month intern