KatInterview

Best Island Total with Row-Major Boundary Cells

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphsgriddfsconnected-components

Last updated

Find connected components in a signed grid and pick the best-scoring one, with and without a no-negatives rule, reporting its first and last row-major cells.

Best Island Report

A grid of integers describes a map. A cell with value 0 is water, and any other value (positive or negative) is land. Two land cells are neighbors if they share a side (up, down, left, right); diagonals do not count. An island is a maximal group of land cells connected through neighbors. The total of an island is the sum of its cell values, and totals can be negative.

Cells are ordered in row-major order: (r1, c1) comes before (r2, c2) if r1 < r2, or if r1 == r2 and c1 < c2.

For each island, define:

  • its first cell: the earliest of its cells in row-major order,
  • its last cell: the latest of its cells in row-major order.

Selection rule

From a set of candidate islands, choose the one with the largest total. If several share the largest total, choose the one whose last cell is latest in row-major order. Distinct islands never share a cell, so exactly one island is chosen.

Task

Write a function that makes the selection twice:

  1. "all": candidates are all islands.
  2. "nonnegative": candidates are only islands in which every cell value is >= 0 (since 0 is water, this means every cell is positive). An island with even one negative cell is excluded entirely; you cannot drop the negative cell and keep the rest.

For each selection, report the chosen island's total, its first cell, and its last cell.

Output format

Return a dictionary with two keys, "all" and "nonnegative". Each maps to a dictionary with keys:

  • "sum": the island's total (integer),
  • "index": the island's first cell as [r, c] (0-based),
  • "last_index": the island's last cell as [r, c] (0-based).

If a selection has no candidate island (the grid is empty, has no land, or, for "nonnegative", every island contains a negative cell), then all three values for that selection are None.

Constraints

  • The grid has m rows and n columns, where m and n can each be 0 (an empty grid is possible).
  • All rows have the same length.
  • Each cell is an integer; 0 is water and any other value is land.
  • A grid can be large enough that deep recursion is risky, so prefer an iterative traversal.
  • Do not rely on the input being a particular shape beyond the above.

Examples

Example 1

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

Output: {'all': {'sum': 5, 'index': [2, 2], 'last_index': [2, 2]}, 'nonnegative': {'sum': 5, 'index': [2, 2], 'last_index': [2, 2]}}

Example 2

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

Output: {'all': {'sum': 5, 'index': [2, 2], 'last_index': [2, 2]}, 'nonnegative': {'sum': 5, 'index': [2, 2], 'last_index': [2, 2]}}

Example 3

Input: largest_island_report([[0, 0], [0, 0]])

Output: {'all': {'sum': None, 'index': None, 'last_index': None}, 'nonnegative': {'sum': None, 'index': None, 'last_index': None}}

Hints

Hint 1

Treat each island as a connected component: scan the grid, and whenever you meet unvisited land, explore the whole island with a stack or queue so each cell is processed once.

Hint 2

While exploring an island, track four things: the running sum, whether any cell is negative, and the minimum and maximum (row, column) pairs seen. Python tuple comparison matches row-major order.

Hint 3

Keep two running bests, one over all islands and one over only islands without negative cells. Compare by total first, then by last cell. Return None fields when a best never gets set.