KatInterview

Count Squares Spanned by Crossing Vertical and Horizontal Segments

Arrays & HashingAuto-graded · 27 test cases
MediumAuto-gradedArrays & Hashinggeometrybitmaskhashing

Last updated

Count axis-aligned squares whose four corners all lie where a vertical segment meets a horizontal one, using per-row bitmasks and pairwise row comparison.

You are given two lists of straight segments on a plane: one list of vertical segments and one list of horizontal segments. Each segment is given by two endpoints, and each endpoint is an [x, y] pair of integers.

A point is a crossing point if it lies on at least one vertical segment and on at least one horizontal segment. Segments are closed, so their endpoints count as part of them. A vertical segment that stops exactly on a horizontal one still creates a crossing point.

Your task is to count the axis-aligned squares of positive side length whose four corners are all crossing points.

Details

  • vert_segs[i] = [[x1, y1], [x2, y2]] with x1 == x2 and y1 != y2.
  • horiz_segs[j] = [[x1, y1], [x2, y2]] with y1 == y2 and x1 != x2.
  • The two endpoints of a segment may be listed in either order.
  • A square is described by a lower-left corner (x, y) and a side s > 0. Its corners are (x, y), (x + s, y), (x, y + s) and (x + s, y + s), and all four must be crossing points.
  • Only squares count. Rectangles with unequal sides do not.
  • Each distinct square is counted once. Squares of every size count, including ones that contain smaller squares.
  • Only the four corners need to be crossing points. Because x-coordinates of vertical segments are distinct and y-coordinates of horizontal segments are distinct, each side of a counted square automatically lies on given segments.

Return the number of such squares as an integer.

Constraints

  • 1 <= len(vert_segs) <= 300
  • 1 <= len(horiz_segs) <= 300
  • All coordinates are integers in [-10^6, 10^6].
  • No two vertical segments share the same x-coordinate.
  • No two horizontal segments share the same y-coordinate.
  • The answer fits in a 64-bit integer.

Examples

Example 1

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

Output: 1

Example 2

Input: count_grid_squares([[[0, 0], [0, 2]], [[1, 0], [1, 2]], [[2, 0], [2, 2]]], [[[0, 0], [2, 0]], [[0, 1], [2, 1]], [[0, 2], [2, 2]]])

Output: 5

Example 3

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

Output: 2

Hints

Hint 1

Every crossing point is determined by one vertical segment (its x) and one horizontal segment (its y). Start by working out, for each horizontal segment, exactly which verticals it meets, remembering that touching at an endpoint counts.

Hint 2

A square is fixed by its bottom row, top row and left column. For a pair of rows with gap s, you need columns x where both rows have crossings at x and at x + s. Think of each row as a set of x positions.

Hint 3

Store each row's crossing x positions as a bitmask. For two rows, AND the masks, then AND the result with itself shifted right by s, and count set bits. Compressing coordinates (subtracting minimums, dividing by one shared gcd) keeps the masks short.