-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3619-CountIslandsWithTotalValueDivisibleByK.go
More file actions
153 lines (140 loc) · 5.22 KB
/
3619-CountIslandsWithTotalValueDivisibleByK.go
File metadata and controls
153 lines (140 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
// 3619. Count Islands With Total Value Divisible by K
// You are given an m x n matrix grid and a positive integer k.
// An island is a group of positive integers (representing land) that are 4-directionally connected (horizontally or vertically).
// The total value of an island is the sum of the values of all cells in the island.
// Return the number of islands with a total value divisible by k.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2025/03/06/example1griddrawio-1.png" />
// Input: grid = [[0,2,1,0,0],[0,5,0,0,5],[0,0,1,0,0],[0,1,4,7,0],[0,2,0,0,8]], k = 5
// Output: 2
// Explanation:
// The grid contains four islands. The islands highlighted in blue have a total value that is divisible by 5, while the islands highlighted in red do not.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2025/03/06/example2griddrawio.png" />
// Input: grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3
// Output: 6
// Explanation:
// The grid contains six islands, each with a total value that is divisible by 3.
// Constraints:
// m == grid.length
// n == grid[i].length
// 1 <= m, n <= 1000
// 1 <= m * n <= 10^5
// 0 <= grid[i][j] <= 10^6
// 1 <= k <= 10^6
import "fmt"
func countIslands(grid [][]int, k int) int {
res, m, n := 0, len(grid), len(grid[0])
var dfs func(i, j int) int
dfs = func(i,j int) int {
paths := [][]int{
{i + 1, j},
{i - 1, j},
{i, j + 1},
{i, j - 1},
}
val := grid[i][j]
grid[i][j] = 0
for _, path := range paths {
r, c := path[0], path[1]
if -1 < r && r < m && -1 < c && c < n && grid[r][c] != 0 {
val += dfs(r, c)
}
}
return val
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] != 0 {
total := dfs(i,j)
if total % k == 0 {
res++
}
}
}
}
return res
}
func countIslands1(grid [][]int, k int) int {
res, m, n := 0, len(grid), len(grid[0])
var dfs func( i, j int, count *int)
dfs = func( i, j int, count *int) {
if i < 0 || j < 0 || i >= m || j >= n || grid[i][j] <= 0 { return }
*count = *count + grid[i][j]
grid[i][j]=-1
dfs(i-1,j,count)
dfs(i,j+1,count)
dfs(i+1,j,count)
dfs(i,j-1,count)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
count := 0
if grid[i][j] > 0 {
dfs(i, j, &count)
if count % k == 0 {
res++
}
}
}
}
return res
}
func countIslands2(grid [][]int, k int) int {
m, n := len(grid), len(grid[0])
if m == 0 { return 0 }
res, total := 0, m * n
visited, stack := make([]bool, total),[]int{}
dirs := [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
indexOf := func(x, y int) int { return x * n + y }
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 0 { continue }
index := indexOf(i, j)
if visited[index] { continue }
visited[index] = true
stack = append(stack, index)
sum := 0
for len(stack) > 0 {
cur := stack[len(stack) - 1]
stack = stack[:len(stack) - 1]
x, y := cur / n, cur % n
sum += grid[x][y]
for _, d := range dirs {
nx, ny := x+d[0], y+d[1]
if nx < 0 || nx >= m || ny < 0 || ny >= n { continue }
if grid[nx][ny] == 0 { continue }
nindex := indexOf(nx, ny)
if visited[nindex] { continue }
visited[nindex] = true
stack = append(stack, nindex)
}
}
if sum % k == 0 {
res++
}
}
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2025/03/06/example1griddrawio-1.png" />
// Input: grid = [[0,2,1,0,0],[0,5,0,0,5],[0,0,1,0,0],[0,1,4,7,0],[0,2,0,0,8]], k = 5
// Output: 2
// Explanation:
// The grid contains four islands. The islands highlighted in blue have a total value that is divisible by 5, while the islands highlighted in red do not.
fmt.Println(countIslands([][]int{{0,2,1,0,0},{0,5,0,0,5},{0,0,1,0,0},{0,1,4,7,0},{0,2,0,0,8}}, 5)) // 2
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2025/03/06/example2griddrawio.png" />
// Input: grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3
// Output: 6
// Explanation:
// The grid contains six islands, each with a total value that is divisible by 3.
fmt.Println(countIslands([][]int{{3,0,3,0}, {0,3,0,3}, {3,0,3,0}}, 3)) // 6
fmt.Println(countIslands1([][]int{{0,2,1,0,0},{0,5,0,0,5},{0,0,1,0,0},{0,1,4,7,0},{0,2,0,0,8}}, 5)) // 2
fmt.Println(countIslands1([][]int{{3,0,3,0}, {0,3,0,3}, {3,0,3,0}}, 3)) // 6
fmt.Println(countIslands2([][]int{{0,2,1,0,0},{0,5,0,0,5},{0,0,1,0,0},{0,1,4,7,0},{0,2,0,0,8}}, 5)) // 2
fmt.Println(countIslands2([][]int{{3,0,3,0}, {0,3,0,3}, {3,0,3,0}}, 3)) // 6
}