iT邦幫忙

2025 iThome 鐵人賽

DAY 25
0
自我挑戰組

Leetcode 小白練功坊系列 第 25

[DAY25] 778. Swim in Rising Water

  • 分享至 

  • xImage
  •  

題目連結(https://leetcode.com/problems/swim-in-rising-water/description/?envType=daily-question&envId=2025-10-06)

You are given an n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (i, j).

It starts raining, and water gradually rises over time. At time t, the water level is t, meaning any cell with elevation less than equal to t is submerged or reachable.

You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t. You can swim infinite distances in zero time. Of course, you must stay within the boundaries of the grid during your swim.

Return the minimum time until you can reach the bottom right square (n - 1, n - 1) if you start at the top left square (0, 0).

這週的每日一題比上次的三角型數學題難很多,有關 water flow,用最短路徑(SP) , minmax, dfs, bfs 解法的問題。

1. Repeat(題目重複確認)

  • 輸入:n*n 的二維方陣 grid ,在時間 t 的水位也是 t,表示棋盤上數字 ≤ t 的位置可抵達
  • 輸出:從左上到右下的最短的時間
  • 前提:
    • n == grid.length, n == grid[i].length
    • 1 <= n <= 50
    • 0 <= grid[i][j] < n^2
    • Each value grid[i][j] is unique. 方陣的每個數字都是獨立的

2. Examples(舉例確認理解)

  _____
  |0|2|
  |1|3|
  _____
Input: grid = [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0). 四個方向都到不了,t=1時可以到(1,0)高度≤1的格子能游,t=2時可以到(0,1)高度≤2的格子能游
You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.


3. Approach(解題思路)

方法 1:priority_queue + Dijkstra

  • 定義往下為(1, 0) 往右為(0, 1)
  • 關鍵想法
    • 時間 t 時,所有高度 ≤ t 的格子都可以游泳到達
    • 目標:要找最小的 t,使得從 (0,0) 能到達 (n-1, n-1)
    • 實際上是在找一條路徑,使得路徑上最大高度最小(minmax問題)
  • priority_queue 策略
    • 總是優先探索高度較小的格子
    • 保證第一次到達終點就是最優解
  • 時間複雜度:O(n² log n)
  • 空間複雜度:O(n²)

方法 2:二分搜尋 + BFS

  • 時間複雜度:O(n² log n²)
  • 空間複雜度:O(n²)

4. Code(C++)

class Solution {
public:
    int swimInWater(vector<vector<int>>& grid) {
        int n = grid.size();
        vector<vector<int>> directions = {{-1,0}, {1,0}, {0,-1}, {0,1}};
        //                                  上      下     左      右
        // {高度, {行, 列}} - 最小堆
        priority_queue<pair<int, pair<int,int>>, 
                      vector<pair<int, pair<int,int>>>,
                      greater<>> pq;
        //二維陣列
        vector<vector<bool>> visited(n, vector<bool>(n, false));
        
        pq.push({grid[0][0], {0, 0}});
        visited[0][0] = true;
        
        int maxHeight = 0;
        
        while (!pq.empty()) {
            auto [height, pos] = pq.top(); //取得頂端元素(不移除)
            auto [row, col] = pos;
            pq.pop();//先用top()取再移除
            
            maxheight = max(maxheight, height);
            
            // 到達終點
            if (row == n-1 && col == n-1) {
                return maxheight;  // 第一次到達就是最優解
            }
            
            // 探索四個方向
            for (auto& dir : directions) {
                int newRow = row + dir[0];
                int newCol = col + dir[1];
                // 邊界檢查
                if (newRow < 0 || newRow >= n || 
                    newCol < 0 || newCol >= n) {
                    continue;
                }
                
                if (visited[newRow][newCol]) {
                    continue;
                }
                
                visited[newRow][newCol] = true; //合法&未訪問
                pq.push({grid[newRow][newCol], {newRow, newCol}});
            }
        }
        
        return -1;
    }
};

5. Test 範例

grid = [[0,2],
        [1,3]]

Step 1: pq = [(0, {0,0})]
        處理 (0,0),高度=0
        加入鄰居:(1,0)高度1, (0,1)高度2

Step 2: pq = [(1, {1,0}), (2, {0,1})]
        處理 (1,0),高度=1,maxHeight=1
        加入鄰居:(1,1)高度3

Step 3: pq = [(2, {0,1}), (3, {1,1})]
        處理 (0,1),高度=2,maxHeight=2

Step 4: pq = [(3, {1,1})]
        處理 (1,1),高度=3,maxHeight=3
        到達終點!返回 3

6. 相關題目與延伸概念

7. 補充:語法&注意事項

1. Priority Queue

// 最小堆(小的在前)- 本題使用
priority_queue
    pair<int, pair<int,int>>,           // 元素類型
    vector<pair<int, pair<int,int>>>,   // 容器
    greater<>                           // 比較器:最小堆
> pq;

// 操作
pq.push({5, {2, 3}});  // 插入 {高度5, 位置(2,3)}
auto top = pq.top();   // 取得最小元素(不移除)
pq.pop();              // 移除最小元素
bool empty = pq.empty(); // 檢查是否為空
----------------------------------------------------------
push(element)     O(log n)  插入元素
top()             O(1)      取得頂端元素(不移除)
pop()             O(log n)  移除頂端元素
empty()           O(1)      檢查是否為空
size()            O(1)      取得元素數量

2. 陣列 visited 宣告

vector<vector<bool>> visited(n, vector<bool>(n, false));
        //                   ↑          ↑    ↑      ↑
        //                外層大小  內層類型 內層大小 初值

3. Range-based For Loop

for (auto dir : directions)     // 複製 4 次,慢
for (auto& dir : directions)    // 不複製,快!
for (const auto& dir : ...)     // 不複製 + 安全

ps. 部分內容經 AI 協助整理


上一篇
[DAY24] 24. Swap Nodes in Pairs
系列文
Leetcode 小白練功坊25
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言