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

LeetCode 2392: Build a Matrix With Conditions

·1 分鐘· ·
LeetCode Hard Graph Topological-Sort Matrix
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: hard 第一次嘗試:2026-08-01

  • 提交紀錄:Accepted ×2(第二次只是拿掉 debug 的 print)

解題思路
#

每個數字剛好出現一次,k × k 的矩陣有 k 列 k 欄,所以可以讓每一列、每一欄都剛好放一個數字。

  • 列條件只決定「誰在上面」,欄條件只決定「誰在左邊」,兩者互不影響。
  • 對列條件做一次 Topological Sort,得到每個數字的列;欄條件也做一次,得到每個數字的欄。
  • 數字 x 放在 (row_pos[x], col_pos[x])。任一邊有環(排不出 k 個)就回傳空矩陣。

我的解法
#

我當時用 heap 取 in-degree 為 0 的點,還加了 seen;一般的 queue 就夠了:

class Solution:
    def buildMatrix(self, k: int, rowConditions: List[List[int]], colConditions: List[List[int]]) -> List[List[int]]:
        def order(conditions):
            adj = [[] for _ in range(k + 1)]
            indeg = [0] * (k + 1)
            for u, v in conditions:
                adj[u].append(v)
                indeg[v] += 1
            q = deque(x for x in range(1, k + 1) if indeg[x] == 0)
            res = []
            while q:
                x = q.popleft()
                res.append(x)
                for y in adj[x]:
                    indeg[y] -= 1
                    if indeg[y] == 0:
                        q.append(y)
            return res if len(res) == k else None   # 有環

        rows, cols = order(rowConditions), order(colConditions)
        if rows is None or cols is None:
            return []
        col_pos = {x: j for j, x in enumerate(cols)}
        mat = [[0] * k for _ in range(k)]
        for i, x in enumerate(rows):
            mat[i][col_pos[x]] = x
        return mat

複雜度
#

時間 O(k² + n + m)(建矩陣是 k²),空間 O(k² + n + m)。

要特別避免的錯誤
#

  • 有環時要回傳空矩陣,不是回傳部分結果。
  • 數字是 1 到 k,陣列大小要開 k + 1(或全部減 1)。

面試口說整理
#

列和欄的條件互不影響,所以我對兩組條件各做一次拓撲排序,每個數字拿到自己的列和欄,因為每個數字只出現一次,每列每欄放一個剛好放得下。任一邊排不出 k 個代表有環,回傳空矩陣。時間 O(k² + n + m)。

相關文章

LeetCode 2603: Collect Coins in a Tree
·2 分鐘
LeetCode Hard Tree Graph Topological-Sort
LeetCode 2603 解題筆記:先剪掉沒有硬幣的葉子,再固定剪兩層,剩下的邊走兩次
LeetCode 269: Alien Dictionary
·2 分鐘
LeetCode Hard Graph Topological-Sort
LeetCode 269 解題筆記,依照原始 learning note 重新整理
LeetCode 1203: Sort Items by Groups Respecting Dependencies
·2 分鐘
LeetCode Hard Graph Topological-Sort
LeetCode 1203 解題筆記,依照原始 learning note 重新整理
LeetCode 3548: Equal Sum Grid Partition II
·2 分鐘
LeetCode Hard Prefix-Sum Hash-Table Matrix Enumeration
LeetCode 3548 解題筆記:枚舉橫切(直切用轉置),兩邊差值要等於較大那邊某一格的值;那一邊只有一列或一欄時只能去掉兩端,否則任何一格都可以
LeetCode 2421: Number of Good Paths
·2 分鐘
LeetCode Hard Tree Union-Find Graph Sorting
LeetCode 2421 解題筆記:依值由小到大加入節點,用 Union-Find 數同一群裡同值的節點
LeetCode 980: Unique Paths III
·1 分鐘
LeetCode Hard Backtracking Matrix Dfs
LeetCode 980 解題筆記:網格回溯,原地標記走過的格子,到終點時檢查是否走過所有空格