|
| 1 | +# Rotting Oranges |
| 2 | + |
| 3 | +Page on leetcode: https://leetcode.com/problems/rotting-oranges/ |
| 4 | + |
| 5 | +## Problem Statement |
| 6 | + |
| 7 | +You are given an m x n grid where each cell can have one of three values: |
| 8 | + |
| 9 | +- 0 representing an empty cell, |
| 10 | +- 1 representing a fresh orange, or |
| 11 | +- 2 representing a rotten orange. |
| 12 | + |
| 13 | +Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. |
| 14 | + |
| 15 | +Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1. |
| 16 | + |
| 17 | +### Constraints |
| 18 | + |
| 19 | +- m == grid.length |
| 20 | +- n == grid[i].length |
| 21 | +- 1 <= m, n <= 10 |
| 22 | +- grid[i][j] is 0, 1, or 2. |
| 23 | + |
| 24 | +### Example |
| 25 | + |
| 26 | +``` |
| 27 | +Input: grid = [[2,1,1],[1,1,0],[0,1,1]] |
| 28 | +Output: 4 |
| 29 | +``` |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +- length of time is the depth from a 2 node to it's deepest 1 node |
| 34 | +- traverse BFS and increment depth counter if you find a 2 |
| 35 | +- Need to find the minimum amount of time for all 1's to chang to 2's then find the max of that |
| 36 | +- What happens if I encounter another 2 on the traversal? |
| 37 | +- Possibly wipe from top left to bottom right and then in the reverse direction with DP? |
| 38 | +- Max variable that compares after the second wipe? |
| 39 | + |
| 40 | +### Initial Pseudocode |
| 41 | + |
| 42 | +1. iterate thru i, j |
| 43 | +2. if i, j is 2, check right and bottom |
| 44 | +3. if a value is > 0 and not 2, set to 3, check right and bottom |
| 45 | +4. if a value is > 0 and not 2, increment parent + 1, check right and bottom |
| 46 | +5. iterate thru i,j towards top left |
| 47 | +6. if i, j is 2, check top, left |
| 48 | +7. if a value is > 0 and not 2, set to 3, check against max, check top, left |
| 49 | +8. if a value is > 0 and not 2, increment parent + 1, check against max, check top, left |
| 50 | +9. return max |
| 51 | + |
| 52 | +### Optimized Solution |
| 53 | + |
| 54 | +The below approach is with BFS and has a time and space complexity of O(mn). You can see an explanation of the approach here: https://www.youtube.com/watch?v=y704fEOx0s0 |
| 55 | + |
| 56 | +```javascript |
| 57 | +const orangesRotting = function (grid) { |
| 58 | + const queue = []; |
| 59 | + let time = 0, |
| 60 | + fresh = 0; |
| 61 | + const ht = grid.length; |
| 62 | + const wd = grid[0].length; |
| 63 | + |
| 64 | + // Iterate thru matrix and find all fresh and rotten oranges |
| 65 | + for (let i = 0; i < ht; i++) { |
| 66 | + for (let j = 0; j < wd; j++) { |
| 67 | + if (grid[i][j] === 2) { |
| 68 | + queue.push([i, j]); |
| 69 | + } else if (grid[i][j] === 1) { |
| 70 | + fresh++; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Directions for checking around an orange in the below loop |
| 76 | + const dir = [ |
| 77 | + [0, 1], |
| 78 | + [1, 0], |
| 79 | + [0, -1], |
| 80 | + [-1, 0], |
| 81 | + ]; |
| 82 | + |
| 83 | + // Work thru rotten oranges in queue |
| 84 | + while (queue.length > 0 && fresh > 0) { |
| 85 | + let level = queue.length; |
| 86 | + |
| 87 | + // Work thru this level of oranges |
| 88 | + while (level > 0) { |
| 89 | + const [row, col] = queue.shift(); |
| 90 | + |
| 91 | + // Check all four directions |
| 92 | + for (let d of dir) { |
| 93 | + const r = row + d[0]; |
| 94 | + const c = col + d[1]; |
| 95 | + const bounds = r >= 0 && r < ht && c >= 0 && c < wd; |
| 96 | + |
| 97 | + if (bounds && grid[r][c] === 1) { |
| 98 | + // If orange satisfies conditions add to queue, change to rotten and remove from fresh counter. |
| 99 | + queue.push([r, c]); |
| 100 | + grid[r][c] = 2; |
| 101 | + fresh--; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + level--; |
| 106 | + } |
| 107 | + time++; |
| 108 | + } |
| 109 | + |
| 110 | + // Check that we turned all fresh oranges rotten |
| 111 | + return fresh === 0 ? time : -1; |
| 112 | +}; |
| 113 | +``` |
0 commit comments