Skip to content

Commit ae77af9

Browse files
Merge branch 'main' into main
2 parents 4bcb5ed + 0fe1388 commit ae77af9

File tree

7 files changed

+421
-8
lines changed

7 files changed

+421
-8
lines changed

solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README.md

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3619.Co
6565

6666
<!-- solution:start -->
6767

68-
### 方法一
68+
### 方法一:DFS
69+
70+
我们定义一个函数 $\textit{dfs}(i, j)$,它从位置 $(i, j)$ 开始进行 DFS 遍历,并且返回该岛屿的总价值。我们将当前位置的值加入总价值,然后将该位置标记为已访问(例如,将其值设为 0)。接着,我们递归地访问四个方向(上、下、左、右)的相邻位置,如果相邻位置的值大于 0,则继续进行 DFS,并将其值加入总价值。最后,我们返回总价值。
71+
72+
在主函数中,我们遍历整个网格,对于每个未访问的位置 $(i, j)$,如果其值大于 0,则调用 $\textit{dfs}(i, j)$ 来计算该岛屿的总价值。如果总价值可以被 $k$ 整除,则将答案加一。
73+
74+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是网格的行数和列数。
6975

7076
<!-- tabs:start -->
7177

7278
#### Python3
7379

7480
```python
75-
81+
class Solution:
82+
def countIslands(self, grid: List[List[int]], k: int) -> int:
83+
def dfs(i: int, j: int) -> int:
84+
s = grid[i][j]
85+
grid[i][j] = 0
86+
for a, b in pairwise(dirs):
87+
x, y = i + a, j + b
88+
if 0 <= x < m and 0 <= y < n and grid[x][y]:
89+
s += dfs(x, y)
90+
return s
91+
92+
m, n = len(grid), len(grid[0])
93+
dirs = (-1, 0, 1, 0, -1)
94+
ans = 0
95+
for i in range(m):
96+
for j in range(n):
97+
if grid[i][j] and dfs(i, j) % k == 0:
98+
ans += 1
99+
return ans
76100
```
77101

78102
#### Java
79103

80104
```java
81-
105+
class Solution {
106+
private int m;
107+
private int n;
108+
private int[][] grid;
109+
private final int[] dirs = {-1, 0, 1, 0, -1};
110+
111+
public int countIslands(int[][] grid, int k) {
112+
m = grid.length;
113+
n = grid[0].length;
114+
this.grid = grid;
115+
int ans = 0;
116+
for (int i = 0; i < m; ++i) {
117+
for (int j = 0; j < n; ++j) {
118+
if (grid[i][j] > 0 && dfs(i, j) % k == 0) {
119+
++ans;
120+
}
121+
}
122+
}
123+
return ans;
124+
}
125+
126+
private long dfs(int i, int j) {
127+
long s = grid[i][j];
128+
grid[i][j] = 0;
129+
for (int d = 0; d < 4; ++d) {
130+
int x = i + dirs[d], y = j + dirs[d + 1];
131+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
132+
s += dfs(x, y);
133+
}
134+
}
135+
return s;
136+
}
137+
}
82138
```
83139

84140
#### C++
85141

86142
```cpp
87-
143+
class Solution {
144+
public:
145+
int countIslands(vector<vector<int>>& grid, int k) {
146+
int m = grid.size(), n = grid[0].size();
147+
vector<int> dirs = {-1, 0, 1, 0, -1};
148+
149+
auto dfs = [&](this auto&& dfs, int i, int j) -> long long {
150+
long long s = grid[i][j];
151+
grid[i][j] = 0;
152+
for (int d = 0; d < 4; ++d) {
153+
int x = i + dirs[d], y = j + dirs[d + 1];
154+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) {
155+
s += dfs(x, y);
156+
}
157+
}
158+
return s;
159+
};
160+
161+
int ans = 0;
162+
for (int i = 0; i < m; ++i) {
163+
for (int j = 0; j < n; ++j) {
164+
if (grid[i][j] && dfs(i, j) % k == 0) {
165+
++ans;
166+
}
167+
}
168+
}
169+
return ans;
170+
}
171+
};
88172
```
89173

90174
#### Go
91175

92176
```go
177+
func countIslands(grid [][]int, k int) (ans int) {
178+
m, n := len(grid), len(grid[0])
179+
dirs := []int{-1, 0, 1, 0, -1}
180+
var dfs func(i, j int) int
181+
dfs = func(i, j int) int {
182+
s := grid[i][j]
183+
grid[i][j] = 0
184+
for d := 0; d < 4; d++ {
185+
x, y := i+dirs[d], j+dirs[d+1]
186+
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0 {
187+
s += dfs(x, y)
188+
}
189+
}
190+
return s
191+
}
192+
for i := 0; i < m; i++ {
193+
for j := 0; j < n; j++ {
194+
if grid[i][j] > 0 && dfs(i, j)%k == 0 {
195+
ans++
196+
}
197+
}
198+
}
199+
return
200+
}
201+
```
93202

203+
#### TypeScript
204+
205+
```ts
206+
function countIslands(grid: number[][], k: number): number {
207+
const m = grid.length,
208+
n = grid[0].length;
209+
const dirs = [-1, 0, 1, 0, -1];
210+
const dfs = (i: number, j: number): number => {
211+
let s = grid[i][j];
212+
grid[i][j] = 0;
213+
for (let d = 0; d < 4; d++) {
214+
const x = i + dirs[d],
215+
y = j + dirs[d + 1];
216+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
217+
s += dfs(x, y);
218+
}
219+
}
220+
return s;
221+
};
222+
223+
let ans = 0;
224+
for (let i = 0; i < m; i++) {
225+
for (let j = 0; j < n; j++) {
226+
if (grid[i][j] > 0 && dfs(i, j) % k === 0) {
227+
ans++;
228+
}
229+
}
230+
}
231+
232+
return ans;
233+
}
94234
```
95235

96236
<!-- tabs:end -->

solution/3600-3699/3619.Count Islands With Total Value Divisible by K/README_EN.md

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3619.Co
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: DFS
67+
68+
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.
69+
70+
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.
71+
72+
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.
6773

6874
<!-- tabs:start -->
6975

7076
#### Python3
7177

7278
```python
73-
79+
class Solution:
80+
def countIslands(self, grid: List[List[int]], k: int) -> int:
81+
def dfs(i: int, j: int) -> int:
82+
s = grid[i][j]
83+
grid[i][j] = 0
84+
for a, b in pairwise(dirs):
85+
x, y = i + a, j + b
86+
if 0 <= x < m and 0 <= y < n and grid[x][y]:
87+
s += dfs(x, y)
88+
return s
89+
90+
m, n = len(grid), len(grid[0])
91+
dirs = (-1, 0, 1, 0, -1)
92+
ans = 0
93+
for i in range(m):
94+
for j in range(n):
95+
if grid[i][j] and dfs(i, j) % k == 0:
96+
ans += 1
97+
return ans
7498
```
7599

76100
#### Java
77101

78102
```java
79-
103+
class Solution {
104+
private int m;
105+
private int n;
106+
private int[][] grid;
107+
private final int[] dirs = {-1, 0, 1, 0, -1};
108+
109+
public int countIslands(int[][] grid, int k) {
110+
m = grid.length;
111+
n = grid[0].length;
112+
this.grid = grid;
113+
int ans = 0;
114+
for (int i = 0; i < m; ++i) {
115+
for (int j = 0; j < n; ++j) {
116+
if (grid[i][j] > 0 && dfs(i, j) % k == 0) {
117+
++ans;
118+
}
119+
}
120+
}
121+
return ans;
122+
}
123+
124+
private long dfs(int i, int j) {
125+
long s = grid[i][j];
126+
grid[i][j] = 0;
127+
for (int d = 0; d < 4; ++d) {
128+
int x = i + dirs[d], y = j + dirs[d + 1];
129+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
130+
s += dfs(x, y);
131+
}
132+
}
133+
return s;
134+
}
135+
}
80136
```
81137

82138
#### C++
83139

84140
```cpp
85-
141+
class Solution {
142+
public:
143+
int countIslands(vector<vector<int>>& grid, int k) {
144+
int m = grid.size(), n = grid[0].size();
145+
vector<int> dirs = {-1, 0, 1, 0, -1};
146+
147+
auto dfs = [&](this auto&& dfs, int i, int j) -> long long {
148+
long long s = grid[i][j];
149+
grid[i][j] = 0;
150+
for (int d = 0; d < 4; ++d) {
151+
int x = i + dirs[d], y = j + dirs[d + 1];
152+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) {
153+
s += dfs(x, y);
154+
}
155+
}
156+
return s;
157+
};
158+
159+
int ans = 0;
160+
for (int i = 0; i < m; ++i) {
161+
for (int j = 0; j < n; ++j) {
162+
if (grid[i][j] && dfs(i, j) % k == 0) {
163+
++ans;
164+
}
165+
}
166+
}
167+
return ans;
168+
}
169+
};
86170
```
87171

88172
#### Go
89173

90174
```go
175+
func countIslands(grid [][]int, k int) (ans int) {
176+
m, n := len(grid), len(grid[0])
177+
dirs := []int{-1, 0, 1, 0, -1}
178+
var dfs func(i, j int) int
179+
dfs = func(i, j int) int {
180+
s := grid[i][j]
181+
grid[i][j] = 0
182+
for d := 0; d < 4; d++ {
183+
x, y := i+dirs[d], j+dirs[d+1]
184+
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0 {
185+
s += dfs(x, y)
186+
}
187+
}
188+
return s
189+
}
190+
for i := 0; i < m; i++ {
191+
for j := 0; j < n; j++ {
192+
if grid[i][j] > 0 && dfs(i, j)%k == 0 {
193+
ans++
194+
}
195+
}
196+
}
197+
return
198+
}
199+
```
91200

201+
#### TypeScript
202+
203+
```ts
204+
function countIslands(grid: number[][], k: number): number {
205+
const m = grid.length,
206+
n = grid[0].length;
207+
const dirs = [-1, 0, 1, 0, -1];
208+
const dfs = (i: number, j: number): number => {
209+
let s = grid[i][j];
210+
grid[i][j] = 0;
211+
for (let d = 0; d < 4; d++) {
212+
const x = i + dirs[d],
213+
y = j + dirs[d + 1];
214+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
215+
s += dfs(x, y);
216+
}
217+
}
218+
return s;
219+
};
220+
221+
let ans = 0;
222+
for (let i = 0; i < m; i++) {
223+
for (let j = 0; j < n; j++) {
224+
if (grid[i][j] > 0 && dfs(i, j) % k === 0) {
225+
ans++;
226+
}
227+
}
228+
}
229+
230+
return ans;
231+
}
92232
```
93233

94234
<!-- tabs:end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int countIslands(vector<vector<int>>& grid, int k) {
4+
int m = grid.size(), n = grid[0].size();
5+
vector<int> dirs = {-1, 0, 1, 0, -1};
6+
7+
auto dfs = [&](this auto&& dfs, int i, int j) -> long long {
8+
long long s = grid[i][j];
9+
grid[i][j] = 0;
10+
for (int d = 0; d < 4; ++d) {
11+
int x = i + dirs[d], y = j + dirs[d + 1];
12+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) {
13+
s += dfs(x, y);
14+
}
15+
}
16+
return s;
17+
};
18+
19+
int ans = 0;
20+
for (int i = 0; i < m; ++i) {
21+
for (int j = 0; j < n; ++j) {
22+
if (grid[i][j] && dfs(i, j) % k == 0) {
23+
++ans;
24+
}
25+
}
26+
}
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)