3619. Count Islands With Total Value Divisible by K Medium

@problem@discussion
#Array#Depth-First Search#Breadth-First Search#Union-Find#Matrix



1/**
2 * [3619] Count Islands With Total Value Divisible by K
3 *
4 * 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).
5 * The total value of an island is the sum of the values of all cells in the island.
6 * Return the number of islands with a total value divisible by k.
7 *  
8 * <strong class="example">Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2025/03/06/example1griddrawio-1.png" style="width: 200px; height: 200px;" />
10 * <div class="example-block">
11 * Input: <span class="example-io">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</span>
12 * Output: <span class="example-io">2</span>
13 * Explanation:
14 * 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.
15 * </div>
16 * <strong class="example">Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2025/03/06/example2griddrawio.png" style="width: 200px; height: 150px;" />
18 * <div class="example-block">
19 * Input: <span class="example-io">grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3</span>
20 * Output: <span class="example-io">6</span>
21 * Explanation:
22 * The grid contains six islands, each with a total value that is divisible by 3.
23 * </div>
24 *  
25 * Constraints:
26 * 
27 * 	m == grid.length
28 * 	n == grid[i].length
29 * 	1 <= m, n <= 1000
30 * 	1 <= m * n <= 10^5
31 * 	0 <= grid[i][j] <= 10^6
32 * 	1 <= k <= 10^6
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/count-islands-with-total-value-divisible-by-k/
38// discuss: https://leetcode.com/problems/count-islands-with-total-value-divisible-by-k/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn count_islands(grid: Vec<Vec<i32>>, k: i32) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_3619() {
56    }
57}
58

Back
© 2026 bowen.ge All Rights Reserved.