Find maximum island sum and required indices
Island Sum and Index Finder
You are given an m x n integer grid where 0 represents water and any non-zero value represents land. Two land cells are considered connected if they share an edge (4-directional). An island is defined as a maximal set of connected land cells.
Your tasks are as follows:
Core Task
- Calculate and return the maximum sum of cell values across all islands found in the grid.
Variants and Additional Requirements
- Variant A (Valid Islands Only): Recompute the maximum sum, but this time, an island is considered valid only if all its constituent cells have non-negative values. Any island containing at least one negative value must be entirely ignored for this calculation. Return this new maximum sum.
- For the island that achieves the maximum sum (from either the core task or Variant A, depending on context), you must also return one index
(r, c)(0-based) of any cell within that island. - Variant B (Specific Index Requirement): Instead of any index, for the island that achieves the maximum sum, return the last index of that island in row-major order. The "last index" refers to the lexicographically largest pair
(r, c)whereris prioritized, thenc. If multiple islands tie for the maximum sum, choose the island whose last index is largest under this same row-major order. - Provide a detailed description of your algorithm and analyze its time and space complexity.
AI interview prep
Core dilemma
Efficiently managing and comparing multiple island properties (sum, validity, specific index) during graph traversal, especially when tie-breaking rules based on lexicographical order are involved, without redundant computation.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find maximum island sum and required indices
Island Sum and Index Finder
You are given an m x n integer grid where 0 represents water and any non-zero value represents land. Two land cells are considered connected if they share an edge (4-directional). An island is defined as a maximal set of connected land cells.
Your tasks are as follows:
Core Task
- Calculate and return the maximum sum of cell values across all islands found in the grid.
Variants and Additional Requirements
- Variant A (Valid Islands Only): Recompute the maximum sum, but this time, an island is considered valid only if all its constituent cells have non-negative values. Any island containing at least one negative value must be entirely ignored for this calculation. Return this new maximum sum.
- For the island that achieves the maximum sum (from either the core task or Variant A, depending on context), you must also return one index
(r, c)(0-based) of any cell within that island. - Variant B (Specific Index Requirement): Instead of any index, for the island that achieves the maximum sum, return the last index of that island in row-major order. The "last index" refers to the lexicographically largest pair
(r, c)whereris prioritized, thenc. If multiple islands tie for the maximum sum, choose the island whose last index is largest under this same row-major order. - Provide a detailed description of your algorithm and analyze its time and space complexity.