Page tree
Skip to end of metadata
Go to start of metadata
Rotting Oranges
class Solution:

        def rotting(target_idx, arr):

            if target_idx < 0 or target_idx >= len(arr) or arr[target_idx] == 0:
                return False

            if arr[target_idx] == 1:
                arr[target_idx] = 2
                return True

        size = len(grid)

        n1_grid = [element for array in grid for element in array]

        on_going = True
        minutes = 0
        direction_indice = [1, -1, size, -1 * size] # 동,서,남,북

        temp = []

        # 1.상한 오렌지 index
        for idx, val in enumerate(n1_grid):
            if val == 2:
                temp.append(idx)


        while on_going:

            changed = False
            next_temp = []

            # 2. 상한 오렌지 전염
            for idx in temp:

                for w in direction_indice:
                    target_idx = idx - w
                    if rotting(target_idx, n1_grid):
                        changed = True
                        next_temp.append(target_idx)

            if changed:
                minutes += 1
            else:
                on_going = False

            temp = next_temp


        return minutes
  • No labels