Number of Distinct Islands (For L5 :)
Updated
Problem statement
In an ocean, there are islands marked by 1. Water is represented by 0.
Determine how many unique shapes (no rotation or mirror) among these islands.
Islands are connected 4-directionally.
Function
numDistinctIslands(grid: int[][]) → int
Complete the function numDistinctIslands in the editor.
numDistinctIslands has the following parameter:
int[][] grid: a 2D array of integers representing the ocean
Returns
int: the number of unique island shapes
Examples
Example 1
grid = [[1, 1, 1, 1, 0, 0], [1, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 1], [1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 0, 0]]return = 4There are 4 unique island shapes:
- the 2 6-sized islands,
- the 2 2-sized islands,
- the 1 2-sized island,
- the 1 1-sized island.
Standard BFS/DFS problem with a translation of the starting point to the origin of each island. The solution is done correctly and optimally with O(n) complexity.
Constraints
1 ≤ grid.length, grid[i].length ≤ 500grid[i][j]is0or1.- Cells are connected only in the four cardinal directions.
- Shapes are equal only under translation; rotations and reflections are distinct.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Number of Distinct IslandsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Query Whether an Island Size ExistsMeta · ONSITE INTERVIEW
- User Journey Paths from a Log StreamWhatNot · PHONE SCREEN
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Sparse Matrix Addition and MultiplicationPinterest · PHONE SCREEN
- Account Roles With Parent-User InheritanceStripe · PHONE SCREEN
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREEN
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREEN