Skip to content
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

feat: add solutions to lc problem: No.0994 #3914

Merged
merged 1 commit into from
Dec 31, 2024
Merged
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
feat: add solutions to lc problem: No.0994
No.0994.Rotting Oranges
yanglbme committed Dec 31, 2024
commit a6f70b3d25c94cdcad8e6ae62b23a8d048be6530
146 changes: 99 additions & 47 deletions solution/0900-0999/0994.Rotting Oranges/README.md
Original file line number Diff line number Diff line change
@@ -92,16 +92,16 @@ tags:
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
q = deque()
cnt = 0
q = deque()
for i, row in enumerate(grid):
for j, x in enumerate(row):
if x == 1:
cnt += 1
elif x == 2:
if x == 2:
q.append((i, j))
dirs = (-1, 0, 1, 0, -1)
elif x == 1:
cnt += 1
ans = 0
dirs = (-1, 0, 1, 0, -1)
while q and cnt:
ans += 1
for _ in range(len(q)):
@@ -112,7 +112,9 @@ class Solution:
grid[x][y] = 2
q.append((x, y))
cnt -= 1
return -1 if cnt > 0 else ans
if cnt == 0:
return ans
return -1 if cnt else 0
```

#### Java
@@ -133,21 +135,22 @@ class Solution {
}
}
final int[] dirs = {-1, 0, 1, 0, -1};
int ans = 0;
for (; !q.isEmpty() && cnt > 0; ++ans) {
for (int ans = 1; !q.isEmpty() && cnt > 0; ++ans) {
for (int k = q.size(); k > 0; --k) {
var p = q.poll();
for (int d = 0; d < 4; ++d) {
int x = p[0] + dirs[d], y = p[1] + dirs[d + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
grid[x][y] = 2;
q.offer(new int[] {x, y});
--cnt;
if (--cnt == 0) {
return ans;
}
}
}
}
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}
}
```
@@ -170,9 +173,8 @@ public:
}
}
}
int ans = 0;
const int dirs[5] = {-1, 0, 1, 0, -1};
for (; q.size() && cnt; ++ans) {
for (int ans = 1; q.size() && cnt; ++ans) {
for (int k = q.size(); k; --k) {
auto [i, j] = q.front();
q.pop();
@@ -181,20 +183,22 @@ public:
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
grid[x][y] = 2;
q.emplace(x, y);
--cnt;
if (--cnt == 0) {
return ans;
}
}
}
}
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}
};
```
#### Go
```go
func orangesRotting(grid [][]int) (ans int) {
func orangesRotting(grid [][]int) int {
m, n := len(grid), len(grid[0])
q := [][2]int{}
cnt := 0
@@ -208,7 +212,7 @@ func orangesRotting(grid [][]int) (ans int) {
}
}
dirs := [5]int{-1, 0, 1, 0, -1}
for ; len(q) > 0 && cnt > 0; ans++ {
for ans := 1; len(q) > 0 && cnt > 0; ans++ {
for k := len(q); k > 0; k-- {
p := q[0]
q = q[1:]
@@ -217,15 +221,17 @@ func orangesRotting(grid [][]int) (ans int) {
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
grid[x][y] = 2
q = append(q, [2]int{x, y})
cnt--
if cnt--; cnt == 0 {
return ans
}
}
}
}
}
if cnt > 0 {
return -1
}
return
return 0
}
```

@@ -246,23 +252,24 @@ function orangesRotting(grid: number[][]): number {
}
}
}
let ans: number = 0;
const dirs: number[] = [-1, 0, 1, 0, -1];
for (; q.length && cnt; ++ans) {
for (let ans = 1; q.length && cnt; ++ans) {
const t: number[][] = [];
for (const [i, j] of q) {
for (let d = 0; d < 4; ++d) {
const [x, y] = [i + dirs[d], j + dirs[d + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === 1) {
grid[x][y] = 2;
t.push([x, y]);
cnt--;
if (--cnt === 0) {
return ans;
}
}
}
}
q.splice(0, q.length, ...t);
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}
```

@@ -277,51 +284,96 @@ impl Solution {
let n = grid[0].len();
let mut q = VecDeque::new();
let mut cnt = 0;

for i in 0..m {
for j in 0..n {
if grid[i][j] == 1 {
cnt += 1;
} else if grid[i][j] == 2 {
q.push_back(vec![i as i32, j as i32]);
q.push_back((i, j));
}
}
}

let dirs: [i32; 5] = [-1, 0, 1, 0, -1];
let mut ans = 0;

while !q.is_empty() && cnt > 0 {
let q_size = q.len();
for _ in 0..q_size {
let p = q.pop_front().unwrap();
let dirs = [-1, 0, 1, 0, -1];
for ans in 1.. {
if q.is_empty() || cnt == 0 {
break;
}
let mut size = q.len();
for _ in 0..size {
let (x, y) = q.pop_front().unwrap();
for d in 0..4 {
let x = p[0] + dirs[d];
let y = p[1] + dirs[d + 1];
if x >= 0
&& x < (m as i32)
&& y >= 0
&& y < (n as i32)
&& grid[x as usize][y as usize] == 1
{
grid[x as usize][y as usize] = 2;
q.push_back(vec![x, y]);
cnt -= 1;
let nx = x as isize + dirs[d] as isize;
let ny = y as isize + dirs[d + 1] as isize;
if nx >= 0 && nx < m as isize && ny >= 0 && ny < n as isize {
let nx = nx as usize;
let ny = ny as usize;
if grid[nx][ny] == 1 {
grid[nx][ny] = 2;
q.push_back((nx, ny));
cnt -= 1;
if cnt == 0 {
return ans;
}
}
}
}
}
ans += 1;
}

if cnt > 0 {
return -1;
-1
} else {
0
}

ans
}
}
```

#### JavaScript

```js
/**
* @param {number[][]} grid
* @return {number}
*/
var orangesRotting = function (grid) {
const m = grid.length;
const n = grid[0].length;
let q = [];
let cnt = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] === 1) {
cnt++;
} else if (grid[i][j] === 2) {
q.push([i, j]);
}
}
}

const dirs = [-1, 0, 1, 0, -1];
for (let ans = 1; q.length && cnt; ++ans) {
let t = [];
for (const [i, j] of q) {
for (let d = 0; d < 4; ++d) {
const x = i + dirs[d];
const y = j + dirs[d + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === 1) {
grid[x][y] = 2;
t.push([x, y]);
if (--cnt === 0) {
return ans;
}
}
}
}
q = [...t];
}

return cnt > 0 ? -1 : 0;
};
```

<!-- tabs:end -->

<!-- solution:end -->
146 changes: 99 additions & 47 deletions solution/0900-0999/0994.Rotting Oranges/README_EN.md
Original file line number Diff line number Diff line change
@@ -88,16 +88,16 @@ The time complexity is $O(m \times n)$, and the space complexity is $O(m \times
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
q = deque()
cnt = 0
q = deque()
for i, row in enumerate(grid):
for j, x in enumerate(row):
if x == 1:
cnt += 1
elif x == 2:
if x == 2:
q.append((i, j))
dirs = (-1, 0, 1, 0, -1)
elif x == 1:
cnt += 1
ans = 0
dirs = (-1, 0, 1, 0, -1)
while q and cnt:
ans += 1
for _ in range(len(q)):
@@ -108,7 +108,9 @@ class Solution:
grid[x][y] = 2
q.append((x, y))
cnt -= 1
return -1 if cnt > 0 else ans
if cnt == 0:
return ans
return -1 if cnt else 0
```

#### Java
@@ -129,21 +131,22 @@ class Solution {
}
}
final int[] dirs = {-1, 0, 1, 0, -1};
int ans = 0;
for (; !q.isEmpty() && cnt > 0; ++ans) {
for (int ans = 1; !q.isEmpty() && cnt > 0; ++ans) {
for (int k = q.size(); k > 0; --k) {
var p = q.poll();
for (int d = 0; d < 4; ++d) {
int x = p[0] + dirs[d], y = p[1] + dirs[d + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
grid[x][y] = 2;
q.offer(new int[] {x, y});
--cnt;
if (--cnt == 0) {
return ans;
}
}
}
}
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}
}
```
@@ -166,9 +169,8 @@ public:
}
}
}
int ans = 0;
const int dirs[5] = {-1, 0, 1, 0, -1};
for (; q.size() && cnt; ++ans) {
for (int ans = 1; q.size() && cnt; ++ans) {
for (int k = q.size(); k; --k) {
auto [i, j] = q.front();
q.pop();
@@ -177,20 +179,22 @@ public:
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
grid[x][y] = 2;
q.emplace(x, y);
--cnt;
if (--cnt == 0) {
return ans;
}
}
}
}
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}
};
```
#### Go
```go
func orangesRotting(grid [][]int) (ans int) {
func orangesRotting(grid [][]int) int {
m, n := len(grid), len(grid[0])
q := [][2]int{}
cnt := 0
@@ -204,7 +208,7 @@ func orangesRotting(grid [][]int) (ans int) {
}
}
dirs := [5]int{-1, 0, 1, 0, -1}
for ; len(q) > 0 && cnt > 0; ans++ {
for ans := 1; len(q) > 0 && cnt > 0; ans++ {
for k := len(q); k > 0; k-- {
p := q[0]
q = q[1:]
@@ -213,15 +217,17 @@ func orangesRotting(grid [][]int) (ans int) {
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
grid[x][y] = 2
q = append(q, [2]int{x, y})
cnt--
if cnt--; cnt == 0 {
return ans
}
}
}
}
}
if cnt > 0 {
return -1
}
return
return 0
}
```

@@ -242,23 +248,24 @@ function orangesRotting(grid: number[][]): number {
}
}
}
let ans: number = 0;
const dirs: number[] = [-1, 0, 1, 0, -1];
for (; q.length && cnt; ++ans) {
for (let ans = 1; q.length && cnt; ++ans) {
const t: number[][] = [];
for (const [i, j] of q) {
for (let d = 0; d < 4; ++d) {
const [x, y] = [i + dirs[d], j + dirs[d + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === 1) {
grid[x][y] = 2;
t.push([x, y]);
cnt--;
if (--cnt === 0) {
return ans;
}
}
}
}
q.splice(0, q.length, ...t);
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}
```

@@ -273,51 +280,96 @@ impl Solution {
let n = grid[0].len();
let mut q = VecDeque::new();
let mut cnt = 0;

for i in 0..m {
for j in 0..n {
if grid[i][j] == 1 {
cnt += 1;
} else if grid[i][j] == 2 {
q.push_back(vec![i as i32, j as i32]);
q.push_back((i, j));
}
}
}

let dirs: [i32; 5] = [-1, 0, 1, 0, -1];
let mut ans = 0;

while !q.is_empty() && cnt > 0 {
let q_size = q.len();
for _ in 0..q_size {
let p = q.pop_front().unwrap();
let dirs = [-1, 0, 1, 0, -1];
for ans in 1.. {
if q.is_empty() || cnt == 0 {
break;
}
let mut size = q.len();
for _ in 0..size {
let (x, y) = q.pop_front().unwrap();
for d in 0..4 {
let x = p[0] + dirs[d];
let y = p[1] + dirs[d + 1];
if x >= 0
&& x < (m as i32)
&& y >= 0
&& y < (n as i32)
&& grid[x as usize][y as usize] == 1
{
grid[x as usize][y as usize] = 2;
q.push_back(vec![x, y]);
cnt -= 1;
let nx = x as isize + dirs[d] as isize;
let ny = y as isize + dirs[d + 1] as isize;
if nx >= 0 && nx < m as isize && ny >= 0 && ny < n as isize {
let nx = nx as usize;
let ny = ny as usize;
if grid[nx][ny] == 1 {
grid[nx][ny] = 2;
q.push_back((nx, ny));
cnt -= 1;
if cnt == 0 {
return ans;
}
}
}
}
}
ans += 1;
}

if cnt > 0 {
return -1;
-1
} else {
0
}

ans
}
}
```

#### JavaScript

```js
/**
* @param {number[][]} grid
* @return {number}
*/
var orangesRotting = function (grid) {
const m = grid.length;
const n = grid[0].length;
let q = [];
let cnt = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] === 1) {
cnt++;
} else if (grid[i][j] === 2) {
q.push([i, j]);
}
}
}

const dirs = [-1, 0, 1, 0, -1];
for (let ans = 1; q.length && cnt; ++ans) {
let t = [];
for (const [i, j] of q) {
for (let d = 0; d < 4; ++d) {
const x = i + dirs[d];
const y = j + dirs[d + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === 1) {
grid[x][y] = 2;
t.push([x, y]);
if (--cnt === 0) {
return ans;
}
}
}
}
q = [...t];
}

return cnt > 0 ? -1 : 0;
};
```

<!-- tabs:end -->

<!-- solution:end -->
11 changes: 6 additions & 5 deletions solution/0900-0999/0994.Rotting Oranges/Solution.cpp
Original file line number Diff line number Diff line change
@@ -13,9 +13,8 @@ class Solution {
}
}
}
int ans = 0;
const int dirs[5] = {-1, 0, 1, 0, -1};
for (; q.size() && cnt; ++ans) {
for (int ans = 1; q.size() && cnt; ++ans) {
for (int k = q.size(); k; --k) {
auto [i, j] = q.front();
q.pop();
@@ -24,11 +23,13 @@ class Solution {
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
grid[x][y] = 2;
q.emplace(x, y);
--cnt;
if (--cnt == 0) {
return ans;
}
}
}
}
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}
};
};
12 changes: 7 additions & 5 deletions solution/0900-0999/0994.Rotting Oranges/Solution.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func orangesRotting(grid [][]int) (ans int) {
func orangesRotting(grid [][]int) int {
m, n := len(grid), len(grid[0])
q := [][2]int{}
cnt := 0
@@ -12,7 +12,7 @@ func orangesRotting(grid [][]int) (ans int) {
}
}
dirs := [5]int{-1, 0, 1, 0, -1}
for ; len(q) > 0 && cnt > 0; ans++ {
for ans := 1; len(q) > 0 && cnt > 0; ans++ {
for k := len(q); k > 0; k-- {
p := q[0]
q = q[1:]
@@ -21,13 +21,15 @@ func orangesRotting(grid [][]int) (ans int) {
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
grid[x][y] = 2
q = append(q, [2]int{x, y})
cnt--
if cnt--; cnt == 0 {
return ans
}
}
}
}
}
if cnt > 0 {
return -1
}
return
}
return 0
}
11 changes: 6 additions & 5 deletions solution/0900-0999/0994.Rotting Oranges/Solution.java
Original file line number Diff line number Diff line change
@@ -13,20 +13,21 @@ public int orangesRotting(int[][] grid) {
}
}
final int[] dirs = {-1, 0, 1, 0, -1};
int ans = 0;
for (; !q.isEmpty() && cnt > 0; ++ans) {
for (int ans = 1; !q.isEmpty() && cnt > 0; ++ans) {
for (int k = q.size(); k > 0; --k) {
var p = q.poll();
for (int d = 0; d < 4; ++d) {
int x = p[0] + dirs[d], y = p[1] + dirs[d + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
grid[x][y] = 2;
q.offer(new int[] {x, y});
--cnt;
if (--cnt == 0) {
return ans;
}
}
}
}
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}
}
}
40 changes: 40 additions & 0 deletions solution/0900-0999/0994.Rotting Oranges/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @param {number[][]} grid
* @return {number}
*/
var orangesRotting = function (grid) {
const m = grid.length;
const n = grid[0].length;
let q = [];
let cnt = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] === 1) {
cnt++;
} else if (grid[i][j] === 2) {
q.push([i, j]);
}
}
}

const dirs = [-1, 0, 1, 0, -1];
for (let ans = 1; q.length && cnt; ++ans) {
let t = [];
for (const [i, j] of q) {
for (let d = 0; d < 4; ++d) {
const x = i + dirs[d];
const y = j + dirs[d + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === 1) {
grid[x][y] = 2;
t.push([x, y]);
if (--cnt === 0) {
return ans;
}
}
}
}
q = [...t];
}

return cnt > 0 ? -1 : 0;
};
14 changes: 8 additions & 6 deletions solution/0900-0999/0994.Rotting Oranges/Solution.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
q = deque()
cnt = 0
q = deque()
for i, row in enumerate(grid):
for j, x in enumerate(row):
if x == 1:
cnt += 1
elif x == 2:
if x == 2:
q.append((i, j))
dirs = (-1, 0, 1, 0, -1)
elif x == 1:
cnt += 1
ans = 0
dirs = (-1, 0, 1, 0, -1)
while q and cnt:
ans += 1
for _ in range(len(q)):
@@ -21,4 +21,6 @@ def orangesRotting(self, grid: List[List[int]]) -> int:
grid[x][y] = 2
q.append((x, y))
cnt -= 1
return -1 if cnt > 0 else ans
if cnt == 0:
return ans
return -1 if cnt else 0
50 changes: 25 additions & 25 deletions solution/0900-0999/0994.Rotting Oranges/Solution.rs
Original file line number Diff line number Diff line change
@@ -6,46 +6,46 @@ impl Solution {
let n = grid[0].len();
let mut q = VecDeque::new();
let mut cnt = 0;

for i in 0..m {
for j in 0..n {
if grid[i][j] == 1 {
cnt += 1;
} else if grid[i][j] == 2 {
q.push_back(vec![i as i32, j as i32]);
q.push_back((i, j));
}
}
}

let dirs: [i32; 5] = [-1, 0, 1, 0, -1];
let mut ans = 0;

while !q.is_empty() && cnt > 0 {
let q_size = q.len();
for _ in 0..q_size {
let p = q.pop_front().unwrap();
let dirs = [-1, 0, 1, 0, -1];
for ans in 1.. {
if q.is_empty() || cnt == 0 {
break;
}
let mut size = q.len();
for _ in 0..size {
let (x, y) = q.pop_front().unwrap();
for d in 0..4 {
let x = p[0] + dirs[d];
let y = p[1] + dirs[d + 1];
if x >= 0
&& x < (m as i32)
&& y >= 0
&& y < (n as i32)
&& grid[x as usize][y as usize] == 1
{
grid[x as usize][y as usize] = 2;
q.push_back(vec![x, y]);
cnt -= 1;
let nx = x as isize + dirs[d] as isize;
let ny = y as isize + dirs[d + 1] as isize;
if nx >= 0 && nx < m as isize && ny >= 0 && ny < n as isize {
let nx = nx as usize;
let ny = ny as usize;
if grid[nx][ny] == 1 {
grid[nx][ny] = 2;
q.push_back((nx, ny));
cnt -= 1;
if cnt == 0 {
return ans;
}
}
}
}
}
ans += 1;
}

if cnt > 0 {
return -1;
-1
} else {
0
}

ans
}
}
9 changes: 5 additions & 4 deletions solution/0900-0999/0994.Rotting Oranges/Solution.ts
Original file line number Diff line number Diff line change
@@ -12,21 +12,22 @@ function orangesRotting(grid: number[][]): number {
}
}
}
let ans: number = 0;
const dirs: number[] = [-1, 0, 1, 0, -1];
for (; q.length && cnt; ++ans) {
for (let ans = 1; q.length && cnt; ++ans) {
const t: number[][] = [];
for (const [i, j] of q) {
for (let d = 0; d < 4; ++d) {
const [x, y] = [i + dirs[d], j + dirs[d + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === 1) {
grid[x][y] = 2;
t.push([x, y]);
cnt--;
if (--cnt === 0) {
return ans;
}
}
}
}
q.splice(0, q.length, ...t);
}
return cnt > 0 ? -1 : ans;
return cnt > 0 ? -1 : 0;
}