KatInterview

Combine Overlapping Ranges and Count Unique Land Shapes

Greedy & IntervalsAuto-graded · 20 test cases
MediumAuto-gradedGreedy & Intervalsintervalssortingdfs

Last updated

Merge overlapping or touching closed ranges, and count land regions in a binary grid that differ in shape when only shifting is allowed.

Combine Overlapping Ranges and Count Unique Land Shapes

You are given two independent inputs, and one function must answer both questions about them.

Part A: Ranges

ranges is a list of closed ranges [start, end], with start <= end. The list may be unsorted and may contain duplicates.

Combine every group of ranges that overlap or touch at an endpoint (for example [1,4] and [4,5] become [1,5]) into a single range. Return the resulting ranges in ascending order of start. The combined ranges must be pairwise disjoint and must not touch each other.

If ranges is empty, the result is an empty list. Do not modify the input.

Part B: Land shapes

grid is a 2D list containing only 0 (water) and 1 (land). A region is a maximal set of land cells connected through their four neighbors (up, down, left, right). Diagonal cells are not connected.

Two regions have the same shape if one can be moved onto the other purely by sliding it (translation). Rotating or mirroring a region does not make it the same shape; a rotated or mirrored copy counts as a different shape unless it also happens to match by translation.

Count how many different shapes appear among the regions. If the grid is empty or contains no land, the count is 0.

Output

Return a dictionary with exactly two keys:

{"merged_intervals": <list of [start, end] lists>, "distinct_islands": <int>}

Constraints

  • 0 <= len(ranges) <= 10^4; each range is [start, end] with integers and start <= end.
  • 0 <= rows, cols <= 50 (grid may be empty); all rows have equal length.
  • Each grid value is 0 or 1.
  • Touching ranges (sharing an endpoint) must be combined.
  • Only 4-directional connectivity is used.
  • The two parts are independent of each other.

Examples

Example 1

Input: merge_ranges_count_shapes([[1, 3], [2, 6], [8, 10], [15, 18]], [[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 1, 1], [0, 0, 0, 1, 1]])

Output: {'merged_intervals': [[1, 6], [8, 10], [15, 18]], 'distinct_islands': 1}

Example 2

Input: merge_ranges_count_shapes([[1, 4], [4, 5]], [[1, 1, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 1, 1], [0, 0, 0, 1, 1]])

Output: {'merged_intervals': [[1, 5]], 'distinct_islands': 2}

Example 3

Input: merge_ranges_count_shapes([], [[1, 1, 0], [0, 0, 1], [1, 1, 0]])

Output: {'merged_intervals': [], 'distinct_islands': 2}

Hints

Hint 1

Sort the ranges by start. After sorting, a range can only overlap the most recently built block, so a single left-to-right pass works.

Hint 2

When extending the last block, set its end to the maximum of the old end and the new end, so a range nested inside a larger one does not shrink it. Merge when start <= last end (touching counts).

Hint 3

For shapes, flood-fill each region starting from the first cell found in row-major order, and store every cell as an offset from that first cell. Put the sorted offsets in a set as a tuple and return the set's size.