KatInterview

Fuse Collinear Point Chains Along Shared Lines

Greedy & IntervalsAuto-graded · 19 test cases
HardAuto-gradedGreedy & Intervalsgeometryintervalshashing

Last updated

Group point lists by the infinite line they lie on, then fuse those that overlap or touch along that line, and return each fused group's unique points ordered along the line.

You are given a list of pieces. Each piece is a list of at least two 2D points [x, y]. Every point inside one piece lies on a single straight line, but the points may be listed in any order, and a piece may contain interior points as well as its two extreme points. A piece may be horizontal, vertical or diagonal.

The extent of a piece is the stretch of its line between its two extreme points.

Two pieces belong together if they lie on the same infinite line and their extents overlap or touch, which includes sharing just one endpoint. This relation is transitive. If A joins B and B joins C, then A, B and C form one group, even if A and C do not meet directly. Pieces that merely cross each other on different lines are never joined.

For every group, output the list of distinct points that occur in any piece of the group, ordered along the line from one end to the other. For a non-vertical line, order by increasing x. For a vertical line, order by increasing y.

Return one such list per group.

Rules for output order and format

  • Groups are returned in order of the smallest original index among the pieces they contain.
  • Each point in the output is a two-element list [x, y].
  • Coordinates are to be treated as exact decimal values. For example, 0.1 means exactly one tenth, so collinearity and equality tests must not be affected by binary floating-point rounding.

Degenerate pieces

A piece whose points are all identical does not define a line. Such a piece is never joined with a piece that has two distinct points, even if it lies on that piece's line. Degenerate pieces with the same location are joined together. Each such location yields a group containing just that single point, positioned by the smallest original index among those pieces.

Constraints

  • 1 <= len(pieces), and each piece has at least 2 points.
  • All points in a piece are collinear.
  • Coordinates are finite decimal numbers (integers or floats with a short decimal representation).
  • Duplicate points may appear within a piece and across pieces.
  • Overlap is inclusive: extents [0, 2] and [2, 5] on the same line are joined.

Examples

Example 1

Input: fuse_collinear_pieces([[[1, 1], [2, 2], [4, 4]], [[4, 2], [2, 1]], [[7, 7], [8, 8]], [[3, 3], [5, 5]]])

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

Example 2

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

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

Example 3

Input: fuse_collinear_pieces([[[0, 0], [1, 1]], [[1, 0], [0, 1]], [[2, 2], [3, 3]]])

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

Hints

Hint 1

Splitting the work helps: first decide which pieces share the same infinite line, then solve a one-dimensional overlap problem on each line.

Hint 2

Build a canonical key for a line that does not depend on which two points you choose. Normalize the coefficients of a*x + b*y + c = 0 by dividing by the first nonzero one, and use exact fractions to avoid float rounding.

Hint 3

Along a line, one coordinate (x, or y for vertical lines) is enough to order points. Sort each line's pieces by their smallest coordinate and sweep, extending the current group while the next piece starts at or before the group's current right end. Track the minimum original index per group for the final ordering.