Day 95 | Rotten Oranges

Day 95 | Rotten Oranges

Today I solved a problem that was practically the same as yesterdays problem with a little variation. You are given an m x n grid where each cell can have one of these 3 values: 0 (representing an empty cell), 1 (representing a fresh orange), or 2 (representing a rotton orange). Every minute, an adjacent orange to a rotten orange becomes rotten. The task is to return the minimum number of minutes until all oranges are rotten. You return -1 if this is not possible.

This problem is practically the same as yesterdays problem, islands and treasure. Instead of distance, we are keeping track of time. Instead of islands and treasure, we have fresh oranges and rotten oranges. The same general idea is the same. What makes this problem a little more difficult is the fact that we have to validate that no fresh oranges exist in the end of the problem. What I mean by this is a rotten orange might end up never reaching a fresh orange. In this case, we'd have to return -1. So the question is, how can we keep track of this?

The Approach

The approach we are going to take is the exact same as yesterday. We are going to perform a bfs from multiple starting points (the rotten oranges). I'm not going to go too deeply into this part since I did yesterday, but lets focus on how we can validate the grid in the end. While initially searching for the rotten oranges to add to our queue, we are also going to be searching for fresh oranges. We can initialize a variable called freshCount. For every fresh orange that we find, we increment this counter. Now when we perform our BFS, for every fresh fruit that turns rotten, we can decrement the counter. Finally, we return -1 if this counter is not 0 and the minutes otherwise.

The Code

class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        ROWS, COLS = len(grid), len(grid[0])
        q = deque()
        freshCount = 0
        for r in range(ROWS):
            for c in range(COLS):
                if grid[r][c] == 2:
                    q.append((r,c))
                if grid[r][c] == 1:
                    freshCount += 1
        minutes = 0
        directions = [[1,0], [0,1], [-1,0], [0,-1]]
        while(q and freshCount):
            for _ in range(len(q)):
                r,c = q.popleft()
                for rowAdd, colAdd in directions:
                    new_r, new_c = r + rowAdd, c + colAdd
                    if new_r >= 0 and new_c >= 0 and new_r < ROWS and new_c < COLS and grid[new_r][new_c] == 1:
                        q.append((new_r,new_c))
                        grid[new_r][new_c] = 2
                        freshCount -= 1
            minutes += 1
        return minutes if freshCount == 0 else -1        

Again, I'm not going to go into the BFS algorithm since I did yesterday, however as you can see, we initialize a freshCount to count the oranges in the grid. For every fresh orange we find, we increment it. Now within the bfs algorithm, every time we move to a cell that contains a fresh orange that turns rotten, we decrement the freshCount to maintain our counter. In the very end, we can return the minutes variable if freshCount is 0, otherwise we return -1 since we can never achieve a board with no fresh oranges.

Complexity Analysis

The time complexity of this solution is going to be O(m x n). This is because we process each cell on the board at most once. The space complexity is also going to be O(m x n) since this is going to be the max size of the queue.

要查看或添加评论,请登录

Haroon Almadani的更多文章

  • Day 98 | Course Schedule

    Day 98 | Course Schedule

    Today's problem was a little different from the other graph problems I've solved so far. While usually I get a grid or…

  • Day 97 | Surrounded Regions

    Day 97 | Surrounded Regions

    In this problem, you are given a 2-D grid. This grid contains "X" an "O" characters.

  • Day 96 | Pacific Atlantic Water Flow

    Day 96 | Pacific Atlantic Water Flow

    Todays graph problem took me a little bit of time to understand. You are given a rectangular island heights where each…

  • Day 94 | Islands and Treasure

    Day 94 | Islands and Treasure

    Today I solved a problem involving a matrix again. You are given a m x n matrix that can consist of only 3 possible…

  • Day 93 | Clone Graph

    Day 93 | Clone Graph

    The last two graph problems I've solved involved an adjacency matrix representation of a graph. This problem was a…

  • Day 92 | Max Area of Islands

    Day 92 | Max Area of Islands

    Today's problem heavily built off yesterdays problem. I recommend solving number of islands first before tackling this…

  • Day 91 | Number of Islands

    Day 91 | Number of Islands

    Today I've moved on from backtracking problems. I'm onto graph problems! This problem involved counting the number of…

  • Day 90 | Letter Combinations of a Phone Number

    Day 90 | Letter Combinations of a Phone Number

    Today's problem was a pretty fun problem. It feels like it's combined everything I've learned in the past couple of…

  • Day 89 | Palindrome Partitioning

    Day 89 | Palindrome Partitioning

    Palindrome Partitioning was a problem that I initially had troubles wrapping my head around. Most backtracking problems…

  • Day 88 | Word Search

    Day 88 | Word Search

    Today I solved a pretty fun problem. I was given a 2-d grid of characters and I was to find out if a word was present…

社区洞察

其他会员也浏览了