Detailed Explanation, Python beat 100% (~350ms)


  • 3
    W

    First of all, please look at my solution to another problem (#694 Number of Distinct Islands)

    Similar to its sister problem, the basic idea is simple:

    1. Scan the matrix, each time when 1 is found, search the nearby 1, push them inside a queue and mark them as -1.

    2. It is important to use the information inside the queue. As we know, the queue stands for a specific shape of an island.

      i. Push the island to the left upper corner by subtracting the original data with its minimum axis value.
      ii. Sort the queue and tuple it to avoid redundancy.
      iii. Find its x-axis mirror: (x,y) --> (-x,y)
      iv. Find its y-axis mirror: (x,y) --> (x,-y)
      v. Find its origin mirror: (x,y) --> (-x,-y)
      vi. Find its diagonal mirror: (x,y) --> (y,x)
      vii. Repeat 2.3, 2.4, and 2.5 on the diagonal mirror island.
      viii. Add all these 8 islands into the pool

    There are several tricks are used here:

    • Augment the original matrix (grid) with a row of zeros and a column of zeros. This is to avoid the check of index every single time

    • Instead of using another matrix to store the visited element, I directly change the value in place. -1 indicates visited. You can use a deep copy if you do not like change value in place.

    • The element in the queue is centered by subtracting the minimum x and minimum y and then sorted, so that this shape of island will be unique.

    class Solution(object):
        def numDistinctIslands2(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            if not grid or not grid[0]: return 0
            m,n=len(grid),len(grid[0])
    
            # augment matrix to void length check
            grid.append([0]*n)
            for row in grid: row.append(0)
    
            self.pool=set()
            self.res=0
    
            def bfs(i0,j0):
                grid[i0][j0]=-1
                q=[(i0,j0)]
                for i,j in q:
                    for I,J in (i-1,j),(i+1,j),(i,j-1),(i,j+1):
                        if grid[I][J]==1:
                            grid[I][J]=-1
                            q.append([I,J])
                self.addisland(q)
           
            for i in range(m):
                for j in range(n):
                    if grid[i][j]==1: bfs(i,j)
    
            return self.res
    
        def addisland(self,q):
                Imin=min(x for x,y in q)
                Jmin=min(y for x,y in q)
                island1=tuple(sorted((x-Imin,y-Jmin) for x,y in q)) # original island
               
                if island1 in self.pool: return None
                self.res+=1
    
                Imax=max(x for x,y in island1)
                Jmax=max(y for x,y in island1)
    
                island2=tuple(sorted((-x+Imax,y) for x,y in island1)) # x axis mirror
                island3=tuple(sorted((x,-y+Jmax) for x,y in island1)) # y axis mirror
                island4=tuple(sorted((-x+Imax,-y+Jmax) for x,y in island1)) # origin mirror
    
                island5=tuple(sorted((y,x) for x,y in island1)) # diagonal mirror
                island6=tuple(sorted((-x+Jmax,y) for x,y in island5))
                island7=tuple(sorted((x,-y+Imax) for x,y in island5))
                island8=tuple(sorted((-x+Jmax,-y+Imax) for x,y in island5))
    
                self.pool|=set([island1,island2,island3,island4,island5,island6,island7,island8])

  • 0
    L

    What's a scenario where we would need island8?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.