Skip to content

Commit e0dbbb1

Browse files
Problem 994 (#39)
* add initial solution * add bfs solution and resources * update readme * add solution and resources * update readme
1 parent e5b0486 commit e0dbbb1

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
4949
38. [Min Stack #155](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/min-stack-155.md)
5050
39. [Validate Binary Search Tree #98](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/validate-binary-tree-98.md)
5151
40. [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
52-
41. Rotting Oranges #994
52+
41. [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)
5353
42. Search in Rotated Sorted Array #33
5454
43. Combination Sum #39
5555
44. Permutations #46
@@ -112,6 +112,7 @@ In order to practice with similar data structures I'll be placing each problem i
112112
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
113113
- [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
114114
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
115+
- [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)
115116

116117
### Queue
117118

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

126128
### Stack
127129

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

225228
### Depth First Search
226229

medium/rotting-oranges-994.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)