← 返回 snapchat 的题目列表Max Area of Island (LC 695)
类型:qbank
Solve LC 695 (Max Area of Island). The first follow-up changes the return value to an integer array containing every island's size; the next asks how to return the maximum size.
Requirements
Start with LeetCode 695, Max Area of Island, as the base problem.
For the first follow-up, change the return value to an integer array containing the size of each island.
For the next follow-up, explain how the function should return the maximum island size.
Clarify any ordering requirement for the array of island sizes before coding; no ordering rule is specified.
Notes
The base problem was identified directly as LC 695. The follow-ups preserve the island traversal but change the result contract from a single value to all component sizes and then back to the maximum.
Scan the grid once. When an unvisited land cell is found, run a four-directional BFS or DFS, mark each land cell when it is discovered, and increment the current component size exactly once per cell. Append that size for the array-returning follow-up; for the maximum-returning forms, either update a running maximum or take the maximum of the collected sizes with a default of 0. If no ordering rule is specified, do not sort the size array unless the interviewer requests it. An all-water grid produces an empty size array and a maximum of 0.
The traversal is O(m * n) time because every cell is processed at most once. Extra space is O(m * n) in the worst case for visited state and the traversal stack or queue.
Preparation
Implement the public base problem, then refactor the result collection so the same traversal can support both follow-up return types.
Practice stating the return contract before coding, especially whether the array of island sizes needs a deterministic order.
Test the base and both follow-up interfaces separately so a return-type change does not leave stale assumptions in the implementation.