Skip to content

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

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 1 commit into from
Dec 31, 2024
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
146 changes: 99 additions & 47 deletions solution/0900-0999/0994.Rotting Oranges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand All @@ -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
Expand All @@ -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;
}
}
```
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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:]
Expand All @@ -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
}
```

Expand All @@ -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;
}
```

Expand All @@ -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 -->
Expand Down
Loading
Loading