Google L5 Video Screen

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2024-12-31Added By Anonymous
Source: Link

Problem: in a ocean there are islands marked by 1. water s represented as 0. Determine how many unique shapes(no rotation or mirror) among these islands. 4-directional connection.


Example input:

[[ 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],

]


Example answer: 4. (the 2 6-sized island, the 2 2-sized island, the 1 2-sized island, the 1 1-sized island)


Standard BFS/DFS problem with a translation of the starting point to origin of each island. I got it done correctly and optimally with O(n) complexity.


The following up is: now allows rotation and mirroring, shapes are considered the same if they can be rotated and mirrored to the same. The only way i can think of is to enumerate all the rotations and mirroring combinations of an island and checking for uniqueness in a set. But i dont feel optimal, and the interviewer hinted by saying how do you normalize a shape.


I did not get to it. How to do normalization of shapes to avoid enumerate all the rotations and mirroring combinations?

Comments