Leetcode 994. Implementation of rotten orange in Python

Time:2021-1-2
  • Title Requirements:

Leetcode 994. Implementation of rotten orange in Python

  • Thinking:

    • Maintain a cur to save the current rotten orange
    • Traverse the given array. If it is a rotten orange, append the abscissa and ordinate to the cur array
    • Define a time to save the time
    • Use the while loop to traverse cur. When there are still elements in cur, continue the loop. Cur stores the array subscript of rotten oranges. Traverse these rotten oranges. When traversing each rotten orange, traverse the upper, lower, left and right four grids of the rotten orange. If there are fresh oranges, change the fresh orange into rotten orange and append the new rotten orange to the nex array
    • After traversing, assign the nex array to the cur array and add time + 1. At this time, there are still elements in cur, so continue the while loop
    • When there are no elements in cur, traverse the grid array to see if there are any fresh oranges in the array. If there are, return – 1. If not, return time
  • Core code:
cur = []
for i in range(len(grid)):
    for j in range(len(grid[0])):
        if grid[i][j] == 2:
            cur.append((i, j))
#Initialize to - 1, because the last traversal in cur saves those oranges in the array that have good oranges turned into rotten oranges. These oranges will be traversed once, but they will not rot other oranges
time = -1
while cur:
    nex = []
    for i, j in cur:
        for x, y in [(0, -1), (-1, 0), (1, 0), (0, 1)]:
            tmp_i = i + x
            tmp_j = j + y
            #Judge the boundary
            if tmp_i >= 0 and tmp_i < len(grid) and tmp_j >= 0 and tmp_j < len(grid[0]):
                if grid[tmp_i][tmp_j] == 1:
                    grid[tmp_i][tmp_j] = 2
                    nex.append((tmp_i,tmp_j))
    time += 1
    cur = nex

for i in grid:
    if 1 in i:
        return -1
return time
  • Full code:

    • In a corner case, if there is only one grid, if there is a 0 or a 2 in the grid, then the time is 0
class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        if len(grid) == 1 and len(grid[-1]) == 1 and grid[0][0] != 1:
            return 0
        cur = []
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == 2:
                    cur.append((i, j))
        time = -1
        while cur:
            nex = []
            for i, j in cur:
                for x, y in [(0, -1), (-1, 0), (1, 0), (0, 1)]:
                    tmp_i = i + x
                    tmp_j = j + y
                    if tmp_i >= 0 and tmp_i < len(grid) and tmp_j >= 0 and tmp_j < len(grid[0]):
                        if grid[tmp_i][tmp_j] == 1:
                            grid[tmp_i][tmp_j] = 2
                            nex.append((tmp_i,tmp_j))
            time += 1
            cur = nex
        
        for i in grid:
            if 1 in i:
                return -1
        return time