KatInterview

Union Overlapping Road Pieces That Share a Straight Line

Greedy & IntervalsAuto-graded · 23 test cases
HardAuto-gradedGreedy & Intervalsintervalsgeometrygcd

Last updated

Group integer-point polylines by the exact line they lie on, then merge overlapping or touching extents on each line into one sorted point list.

You are given a collection of straight road pieces. Each piece is a list of integer points [x, y] that all lie on a single straight line. Several pieces may lie on the same line and may overlap. Combine every group of overlapping pieces on a common line into a single piece that contains all of their points, and return the resulting pieces.

Your function must work for every orientation, including horizontal and vertical pieces.

Rules

  • Two pieces are on the same line when all of their points are collinear. Pieces on different lines are never combined, even if they cross, touch, or are parallel.
  • The span of a piece is the closed stretch of its line running from its first point to its last point. Two pieces on the same line overlap when their spans share at least one point. Sharing just one endpoint counts as overlapping.
  • Combining is transitive: if A overlaps B and B overlaps C, then A, B and C become one piece, even if A and C do not overlap each other.
  • Every output piece, combined or not, is the list of the distinct points from all of its member pieces, sorted by x ascending and then by y ascending.
  • Output order: group output pieces by line, and order the lines by the index of the first input piece that lies on each line. Within one line, order the output pieces by their first point (by x, then by y).

Input

  • pieces: a list of pieces. Each piece is a list of points, and each point is a list [x, y] of two integers.

Output

A list of merged pieces in the order described above. Each piece is a list of [x, y] points.

Constraints

  • 1 <= len(pieces) <= 10^4
  • 2 <= len(pieces[i]) <= 100, and the total number of points over all pieces is at most 10^5.
  • Every coordinate is an integer in [-10^4, 10^4].
  • Within a piece, the points are distinct, collinear, and already sorted by x ascending and then by y ascending.
  • Different pieces may share points or be identical.

Examples

Example 1

Input: merge_road_pieces([[[1, 1], [2, 2], [4, 4]], [[2, 1], [4, 2]], [[3, 3], [6, 6]], [[7, 7], [8, 8]]])

Output: [[[1, 1], [2, 2], [3, 3], [4, 4], [6, 6]], [[7, 7], [8, 8]], [[2, 1], [4, 2]]]

Example 2

Input: merge_road_pieces([[[0, 0], [2, 0]], [[1, -2], [1, 0]], [[2, 0], [5, 0]], [[1, 3], [1, 5]], [[6, 0], [7, 0]]])

Output: [[[0, 0], [2, 0], [5, 0]], [[6, 0], [7, 0]], [[1, -2], [1, 0]], [[1, 3], [1, 5]]]

Example 3

Input: merge_road_pieces([[[0, 0], [2, 1], [6, 3]], [[0, 1], [2, 2]], [[2, 1], [4, 2]]])

Output: [[[0, 0], [2, 1], [4, 2], [6, 3]], [[0, 1], [2, 2]]]

Hints

Hint 1

Avoid floating-point slopes: vertical pieces break the division and rounding can make equal slopes look different. Describe a line with integers only.

Hint 2

Reduce the direction vector between a piece's first two points by their gcd, fix its sign so it is unique, and add an offset such as dy*x0 - dx*y0 so parallel lines get different keys.

Hint 3

After the sign fix, ordering points by (x, y) matches their order along the line, so each piece is an interval you can compare with tuples. Sort by start and sweep, treating start <= current end as overlapping.