Skip to content

Problem 994 #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
38. [Min Stack #155](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/min-stack-155.md)
39. [Validate Binary Search Tree #98](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/validate-binary-tree-98.md)
40. [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
41. Rotting Oranges #994
41. [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)
42. Search in Rotated Sorted Array #33
43. Combination Sum #39
44. Permutations #46
Expand Down Expand Up @@ -112,6 +112,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
- [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
- [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)

### Queue

Expand All @@ -122,6 +123,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
- [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)

### Stack

Expand Down Expand Up @@ -221,6 +223,7 @@ Within the problems above there are several patterns that often occur. I plan to
- [Course Schedule #207](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/course-schedule-207.md)
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
- [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)

### Depth First Search

Expand Down
113 changes: 113 additions & 0 deletions medium/rotting-oranges-994.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Rotting Oranges

Page on leetcode: https://leetcode.com/problems/rotting-oranges/

## Problem Statement

You are given an m x n grid where each cell can have one of three values:

- 0 representing an empty cell,
- 1 representing a fresh orange, or
- 2 representing a rotten orange.

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

### Constraints

- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 10
- grid[i][j] is 0, 1, or 2.

### Example

```
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
```

## Solution

- length of time is the depth from a 2 node to it's deepest 1 node
- traverse BFS and increment depth counter if you find a 2
- Need to find the minimum amount of time for all 1's to chang to 2's then find the max of that
- What happens if I encounter another 2 on the traversal?
- Possibly wipe from top left to bottom right and then in the reverse direction with DP?
- Max variable that compares after the second wipe?

### Initial Pseudocode

1. iterate thru i, j
2. if i, j is 2, check right and bottom
3. if a value is > 0 and not 2, set to 3, check right and bottom
4. if a value is > 0 and not 2, increment parent + 1, check right and bottom
5. iterate thru i,j towards top left
6. if i, j is 2, check top, left
7. if a value is > 0 and not 2, set to 3, check against max, check top, left
8. if a value is > 0 and not 2, increment parent + 1, check against max, check top, left
9. return max

### Optimized Solution

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

```javascript
const orangesRotting = function (grid) {
const queue = [];
let time = 0,
fresh = 0;
const ht = grid.length;
const wd = grid[0].length;

// Iterate thru matrix and find all fresh and rotten oranges
for (let i = 0; i < ht; i++) {
for (let j = 0; j < wd; j++) {
if (grid[i][j] === 2) {
queue.push([i, j]);
} else if (grid[i][j] === 1) {
fresh++;
}
}
}

// Directions for checking around an orange in the below loop
const dir = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];

// Work thru rotten oranges in queue
while (queue.length > 0 && fresh > 0) {
let level = queue.length;

// Work thru this level of oranges
while (level > 0) {
const [row, col] = queue.shift();

// Check all four directions
for (let d of dir) {
const r = row + d[0];
const c = col + d[1];
const bounds = r >= 0 && r < ht && c >= 0 && c < wd;

if (bounds && grid[r][c] === 1) {
// If orange satisfies conditions add to queue, change to rotten and remove from fresh counter.
queue.push([r, c]);
grid[r][c] = 2;
fresh--;
}
}

level--;
}
time++;
}

// Check that we turned all fresh oranges rotten
return fresh === 0 ? time : -1;
};
```