Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3619 - Count Islands With Total Value Divisible by K.

    Medium

    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:

    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:

    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

    • <code>1 <= m * n <= 10<sup>5</sup></code>

    • <code>0 <= gridj<= 10<sup>6</sup></code>

    • <code>1 <= k <= 10<sup>6</sup></code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer countIslands(Array<IntArray> grid, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait