diff --git a/solution/0900-0999/0994.Rotting Oranges/README.md b/solution/0900-0999/0994.Rotting Oranges/README.md index d0ec4e3e88685..a99d00397678b 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README.md +++ b/solution/0900-0999/0994.Rotting Oranges/README.md @@ -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,8 +135,7 @@ 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) { @@ -142,12 +143,14 @@ class Solution { 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,12 +183,14 @@ 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; } }; ``` @@ -194,7 +198,7 @@ public: #### 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,7 +221,9 @@ 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 + } } } } @@ -225,7 +231,7 @@ func orangesRotting(grid [][]int) (ans int) { if cnt > 0 { return -1 } - return + return 0 } ``` @@ -246,9 +252,8 @@ 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) { @@ -256,13 +261,15 @@ function orangesRotting(grid: number[][]): number { 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; +}; +``` + diff --git a/solution/0900-0999/0994.Rotting Oranges/README_EN.md b/solution/0900-0999/0994.Rotting Oranges/README_EN.md index c1482642cdeb6..4aae32c8483e7 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README_EN.md +++ b/solution/0900-0999/0994.Rotting Oranges/README_EN.md @@ -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,8 +131,7 @@ 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) { @@ -138,12 +139,14 @@ class Solution { 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,12 +179,14 @@ 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; } }; ``` @@ -190,7 +194,7 @@ public: #### 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,7 +217,9 @@ 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 + } } } } @@ -221,7 +227,7 @@ func orangesRotting(grid [][]int) (ans int) { if cnt > 0 { return -1 } - return + return 0 } ``` @@ -242,9 +248,8 @@ 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) { @@ -252,13 +257,15 @@ function orangesRotting(grid: number[][]): number { 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; +}; +``` + diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.cpp b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp index c5c21ac6401a1..3498a899e3fbd 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.cpp +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp @@ -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; } -}; \ No newline at end of file +}; diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.go b/solution/0900-0999/0994.Rotting Oranges/Solution.go index ab1d4edfc33a5..928a7f4e2e0f8 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.go +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.go @@ -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,7 +21,9 @@ 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 + } } } } @@ -29,5 +31,5 @@ func orangesRotting(grid [][]int) (ans int) { if cnt > 0 { return -1 } - return -} \ No newline at end of file + return 0 +} diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.java b/solution/0900-0999/0994.Rotting Oranges/Solution.java index 658b248d52af6..5483975622903 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.java +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.java @@ -13,8 +13,7 @@ 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) { @@ -22,11 +21,13 @@ public int orangesRotting(int[][] grid) { 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; } -} \ No newline at end of file +} diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.js b/solution/0900-0999/0994.Rotting Oranges/Solution.js new file mode 100644 index 0000000000000..28a409aa7305c --- /dev/null +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.js @@ -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; +}; diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.py b/solution/0900-0999/0994.Rotting Oranges/Solution.py index 61012d6825f98..5d66db0e9a734 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.py +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.py @@ -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 diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.rs b/solution/0900-0999/0994.Rotting Oranges/Solution.rs index 4e8479ffaadab..8620e44c76591 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.rs +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.rs @@ -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 } } diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.ts b/solution/0900-0999/0994.Rotting Oranges/Solution.ts index ecfeee94b3584..267bce3d28826 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.ts +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.ts @@ -12,9 +12,8 @@ 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) { @@ -22,11 +21,13 @@ function orangesRotting(grid: number[][]): number { 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; }