↓快轉到主要內容
  1. LeetCode/

LeetCode 3619: Count Islands With Total Value Divisible by K

·2 分鐘· ·
LeetCode Medium Dfs Bfs Matrix Graph
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試:2025-07-19

  • 提交紀錄:Accepted(一次通過)

解題思路
#

找出每座島(上下左右相連的正數格子),把島上的值加起來,數有幾座島的總和能被 k 整除。

就是 LC 200 Number of Islands,只是每座島除了計數,還要加總。

我的解法
#

原本是遞迴的 DFS,走過的格子改成 -1:

def search(i, j):
    if i < 0 or i >= n or j >= m or j < 0 or grid[i][j] == -1:
        return 0
    value = grid[i][j]
    if grid[i][j] == 0:
        grid[i][j] = -1
        return 0
    grid[i][j] = -1
    return value + search(i + 1, j) + search(i, j + 1) + search(i - 1, j) + search(i, j - 1)

可以 AC,但網格最多 10^5 格,一座很長的島會讓遞迴超過 Python 預設的上限。改成 stack 迭代比較安全:

class Solution:
    def countIslands(self, grid: List[List[int]], k: int) -> int:
        m, n = len(grid), len(grid[0])
        res = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j] <= 0:
                    continue
                total = grid[i][j]
                grid[i][j] = 0                     # 標記走過
                stack = [(i, j)]
                while stack:
                    r, c = stack.pop()
                    for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
                        if 0 <= nr < m and 0 <= nc < n and grid[nr][nc] > 0:
                            total += grid[nr][nc]
                            grid[nr][nc] = 0
                            stack.append((nr, nc))
                if total % k == 0:
                    res += 1
        return res

把走過的格子直接改成 0,就不需要另外一個 -1 狀態,也不用區分「水」和「走過的陸地」。

複雜度
#

時間 O(mn),空間 O(mn)(stack 最壞情況)。

要特別避免的錯誤
#

  • 遞迴 DFS 在大網格上超過遞迴上限。
  • 推進 stack 之前沒有標記:同一格會被推好幾次、重複加總。

面試口說整理
#

這是 Number of Islands 加上加總。我掃過每個格子,遇到還沒走過的陸地就用 stack 做 flood fill,邊走邊加總並把格子改成 0 當作標記。整座島走完後檢查總和能不能被 k 整除。每格只處理一次,時間 O(mn),用迭代避免大網格的遞迴深度問題。

相關文章

LeetCode 3493: Properties Graph
·1 分鐘
LeetCode Medium Graph Dfs Union-Find Hash-Map
LeetCode 3493 解題筆記:先把每列轉成 set,兩兩比較交集大小建邊,再數連通塊
LeetCode 958: Check Completeness of a Binary Tree
·1 分鐘
LeetCode Medium Tree Binary-Tree Bfs
LeetCode 958 解題筆記:完整性是整體性質,用 BFS 檢查第一個空位之後還有沒有節點
LeetCode 3613: Minimize Maximum Component Cost
·1 分鐘
LeetCode Medium Union-Find Graph Sorting Binary-Search
LeetCode 3613 解題筆記:邊由小到大加進 Union-Find,剛好剩 k 塊時那條邊的權重就是答案;n <= k 時回傳 0
LeetCode 3607: Power Grid Maintenance
·2 分鐘
LeetCode Medium Heap Union-Find Graph Lazy-Deletion
LeetCode 3607 解題筆記:Union-Find 分群,每群一個 min-heap,離線的站用 lazy deletion
LeetCode 3608: Minimum Time for K Connected Components
·1 分鐘
LeetCode Medium Union-Find Graph Sorting Binary-Search
LeetCode 3608 解題筆記:倒過來從時間大的邊開始加回去,第一次讓連通塊數少於 k 的那條邊的時間就是答案
LeetCode 3070: Count Submatrices with Top-Left Element and Sum Less Than k
·1 分鐘
LeetCode Medium Array Matrix Prefix-Sum
LeetCode 3070 解題筆記:包含左上角的子矩陣就是二維前綴和 P[i][j],數有幾個 <= k