diff --git a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README.md b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README.md index ad3505a76ed46..ded52895b260f 100644 --- a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README.md +++ b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README.md @@ -65,32 +65,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3619.Co -### 方法一 +### 方法一:DFS + +我们定义一个函数 $\textit{dfs}(i, j)$,它从位置 $(i, j)$ 开始进行 DFS 遍历,并且返回该岛屿的总价值。我们将当前位置的值加入总价值,然后将该位置标记为已访问(例如,将其值设为 0)。接着,我们递归地访问四个方向(上、下、左、右)的相邻位置,如果相邻位置的值大于 0,则继续进行 DFS,并将其值加入总价值。最后,我们返回总价值。 + +在主函数中,我们遍历整个网格,对于每个未访问的位置 $(i, j)$,如果其值大于 0,则调用 $\textit{dfs}(i, j)$ 来计算该岛屿的总价值。如果总价值可以被 $k$ 整除,则将答案加一。 + +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是网格的行数和列数。 #### Python3 ```python - +class Solution: + def countIslands(self, grid: List[List[int]], k: int) -> int: + def dfs(i: int, j: int) -> int: + s = grid[i][j] + grid[i][j] = 0 + for a, b in pairwise(dirs): + x, y = i + a, j + b + if 0 <= x < m and 0 <= y < n and grid[x][y]: + s += dfs(x, y) + return s + + m, n = len(grid), len(grid[0]) + dirs = (-1, 0, 1, 0, -1) + ans = 0 + for i in range(m): + for j in range(n): + if grid[i][j] and dfs(i, j) % k == 0: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + private int m; + private int n; + private int[][] grid; + private final int[] dirs = {-1, 0, 1, 0, -1}; + + public int countIslands(int[][] grid, int k) { + m = grid.length; + n = grid[0].length; + this.grid = grid; + int ans = 0; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] > 0 && dfs(i, j) % k == 0) { + ++ans; + } + } + } + return ans; + } + + private long dfs(int i, int j) { + long s = grid[i][j]; + grid[i][j] = 0; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) { + s += dfs(x, y); + } + } + return s; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countIslands(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + vector dirs = {-1, 0, 1, 0, -1}; + + auto dfs = [&](this auto&& dfs, int i, int j) -> long long { + long long s = grid[i][j]; + grid[i][j] = 0; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) { + s += dfs(x, y); + } + } + return s; + }; + + int ans = 0; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] && dfs(i, j) % k == 0) { + ++ans; + } + } + } + return ans; + } +}; ``` #### Go ```go +func countIslands(grid [][]int, k int) (ans int) { + m, n := len(grid), len(grid[0]) + dirs := []int{-1, 0, 1, 0, -1} + var dfs func(i, j int) int + dfs = func(i, j int) int { + s := grid[i][j] + grid[i][j] = 0 + for d := 0; d < 4; d++ { + x, y := i+dirs[d], j+dirs[d+1] + if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0 { + s += dfs(x, y) + } + } + return s + } + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] > 0 && dfs(i, j)%k == 0 { + ans++ + } + } + } + return +} +``` +#### TypeScript + +```ts +function countIslands(grid: number[][], k: number): number { + const m = grid.length, + n = grid[0].length; + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i: number, j: number): number => { + let s = grid[i][j]; + grid[i][j] = 0; + for (let d = 0; d < 4; d++) { + const x = i + dirs[d], + y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) { + s += dfs(x, y); + } + } + return s; + }; + + let ans = 0; + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] > 0 && dfs(i, j) % k === 0) { + ans++; + } + } + } + + return ans; +} ``` diff --git a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README_EN.md b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README_EN.md index b47d245a99e1c..dee73c6315c7e 100644 --- a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README_EN.md +++ b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README_EN.md @@ -63,32 +63,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3619.Co -### Solution 1 +### Solution 1: DFS + +We define a function $\textit{dfs}(i, j)$, which performs DFS traversal starting from position $(i, j)$ and returns the total value of that island. We add the current position's value to the total value, then mark that position as visited (for example, by setting its value to 0). Next, we recursively visit the adjacent positions in four directions (up, down, left, right). If an adjacent position has a value greater than 0, we continue the DFS and add its value to the total value. Finally, we return the total value. + +In the main function, we traverse the entire grid. For each unvisited position $(i, j)$, if its value is greater than 0, we call $\textit{dfs}(i, j)$ to calculate the total value of that island. If the total value is divisible by $k$, we increment the answer by one. + +The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the grid, respectively. #### Python3 ```python - +class Solution: + def countIslands(self, grid: List[List[int]], k: int) -> int: + def dfs(i: int, j: int) -> int: + s = grid[i][j] + grid[i][j] = 0 + for a, b in pairwise(dirs): + x, y = i + a, j + b + if 0 <= x < m and 0 <= y < n and grid[x][y]: + s += dfs(x, y) + return s + + m, n = len(grid), len(grid[0]) + dirs = (-1, 0, 1, 0, -1) + ans = 0 + for i in range(m): + for j in range(n): + if grid[i][j] and dfs(i, j) % k == 0: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + private int m; + private int n; + private int[][] grid; + private final int[] dirs = {-1, 0, 1, 0, -1}; + + public int countIslands(int[][] grid, int k) { + m = grid.length; + n = grid[0].length; + this.grid = grid; + int ans = 0; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] > 0 && dfs(i, j) % k == 0) { + ++ans; + } + } + } + return ans; + } + + private long dfs(int i, int j) { + long s = grid[i][j]; + grid[i][j] = 0; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) { + s += dfs(x, y); + } + } + return s; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countIslands(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + vector dirs = {-1, 0, 1, 0, -1}; + + auto dfs = [&](this auto&& dfs, int i, int j) -> long long { + long long s = grid[i][j]; + grid[i][j] = 0; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) { + s += dfs(x, y); + } + } + return s; + }; + + int ans = 0; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] && dfs(i, j) % k == 0) { + ++ans; + } + } + } + return ans; + } +}; ``` #### Go ```go +func countIslands(grid [][]int, k int) (ans int) { + m, n := len(grid), len(grid[0]) + dirs := []int{-1, 0, 1, 0, -1} + var dfs func(i, j int) int + dfs = func(i, j int) int { + s := grid[i][j] + grid[i][j] = 0 + for d := 0; d < 4; d++ { + x, y := i+dirs[d], j+dirs[d+1] + if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0 { + s += dfs(x, y) + } + } + return s + } + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] > 0 && dfs(i, j)%k == 0 { + ans++ + } + } + } + return +} +``` +#### TypeScript + +```ts +function countIslands(grid: number[][], k: number): number { + const m = grid.length, + n = grid[0].length; + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i: number, j: number): number => { + let s = grid[i][j]; + grid[i][j] = 0; + for (let d = 0; d < 4; d++) { + const x = i + dirs[d], + y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) { + s += dfs(x, y); + } + } + return s; + }; + + let ans = 0; + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] > 0 && dfs(i, j) % k === 0) { + ans++; + } + } + } + + return ans; +} ``` diff --git a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.cpp b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.cpp new file mode 100644 index 0000000000000..5ce322250145e --- /dev/null +++ b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int countIslands(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + vector dirs = {-1, 0, 1, 0, -1}; + + auto dfs = [&](this auto&& dfs, int i, int j) -> long long { + long long s = grid[i][j]; + grid[i][j] = 0; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) { + s += dfs(x, y); + } + } + return s; + }; + + int ans = 0; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] && dfs(i, j) % k == 0) { + ++ans; + } + } + } + return ans; + } +}; diff --git a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.go b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.go new file mode 100644 index 0000000000000..74fc51693d965 --- /dev/null +++ b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.go @@ -0,0 +1,24 @@ +func countIslands(grid [][]int, k int) (ans int) { + m, n := len(grid), len(grid[0]) + dirs := []int{-1, 0, 1, 0, -1} + var dfs func(i, j int) int + dfs = func(i, j int) int { + s := grid[i][j] + grid[i][j] = 0 + for d := 0; d < 4; d++ { + x, y := i+dirs[d], j+dirs[d+1] + if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0 { + s += dfs(x, y) + } + } + return s + } + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] > 0 && dfs(i, j)%k == 0 { + ans++ + } + } + } + return +} diff --git a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.java b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.java new file mode 100644 index 0000000000000..901a053339e7f --- /dev/null +++ b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.java @@ -0,0 +1,33 @@ +class Solution { + private int m; + private int n; + private int[][] grid; + private final int[] dirs = {-1, 0, 1, 0, -1}; + + public int countIslands(int[][] grid, int k) { + m = grid.length; + n = grid[0].length; + this.grid = grid; + int ans = 0; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] > 0 && dfs(i, j) % k == 0) { + ++ans; + } + } + } + return ans; + } + + private long dfs(int i, int j) { + long s = grid[i][j]; + grid[i][j] = 0; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) { + s += dfs(x, y); + } + } + return s; + } +} \ No newline at end of file diff --git a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.py b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.py new file mode 100644 index 0000000000000..80dc273d4bc56 --- /dev/null +++ b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.py @@ -0,0 +1,19 @@ +class Solution: + def countIslands(self, grid: List[List[int]], k: int) -> int: + def dfs(i: int, j: int) -> int: + s = grid[i][j] + grid[i][j] = 0 + for a, b in pairwise(dirs): + x, y = i + a, j + b + if 0 <= x < m and 0 <= y < n and grid[x][y]: + s += dfs(x, y) + return s + + m, n = len(grid), len(grid[0]) + dirs = (-1, 0, 1, 0, -1) + ans = 0 + for i in range(m): + for j in range(n): + if grid[i][j] and dfs(i, j) % k == 0: + ans += 1 + return ans diff --git a/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.ts b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.ts new file mode 100644 index 0000000000000..73bd758b5924f --- /dev/null +++ b/solution/3600-3699/3619.Count Islands With Total Value Divisible by K/Solution.ts @@ -0,0 +1,28 @@ +function countIslands(grid: number[][], k: number): number { + const m = grid.length, + n = grid[0].length; + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i: number, j: number): number => { + let s = grid[i][j]; + grid[i][j] = 0; + for (let d = 0; d < 4; d++) { + const x = i + dirs[d], + y = j + dirs[d + 1]; + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) { + s += dfs(x, y); + } + } + return s; + }; + + let ans = 0; + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] > 0 && dfs(i, j) % k === 0) { + ans++; + } + } + } + + return ans; +}