Try   HackMD
Dark Theme License

The MIT License (MIT)

Copyright © 2022-2023 Lumynous

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

題目連結: Leetcode

題目概要

給一個

n×m 的矩陣表示一塊土地territory, 其中若該格是陸地則為 0, 若為積水則為 1
原本整塊土地都是可以通過的, 即全都是 0
若今天因為大雨而導致每天都有格子被積水淹沒, 並用多個座標來表示被淹沒的格子
cells[i] = [ri,ci]
表示在第
i
天時 territory[
ri
][
ci
]會被水淹沒

問若每天都要從這塊土地的最上方穿過土地走到最下方
在第幾天的時候會沒辦法穿過土地

條件:

  • 2
    n, m
    2×104
  • 4
    <=
    n×m
    <=
    2104
  • cells.length == n
    ×
    m
  • 1rirow
  • 1cicol
  • cells 中每項皆不相同, 即不會重複積水

此題cells表示的座標是1開始

sample
cells = {{1, 5}, {1, 1}, {2, 3}, {3, 5}, {1, 3}, {1, 4}, {3, 2}, {2, 1}, {5, 5}, {5, 2}, {5, 4}, {3, 4}, {2, 4}, {1, 2}, {5, 3}, {2, 2}, {4, 4}, {2, 5}, {3, 1}, {4, 5}, {4, 3}, {4, 2}, {3, 3}, {5, 1}, {4, 1}};

output: 7

想法

"以最上方可走的格子作為起點做BFS直到走到最下列" 來檢查能不能穿過這塊土地
考量到若每一天都做一次BFS會需要

O(nm(nm1)!) 來檢查
因此使用二分搜檢查一半的天數 再往上/下 找 直到找到無法通過的最小天數
這樣所需的時間複雜度只有
O(nmlog(nm))

參考解法

歡迎補充

CPP BFS + Binary Search
class Solution { public: vector<pair<int, int>> d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool BFS(int row, int col, vector<vector<int>>& cells, int mid) { vector<vector<int>> table(row, vector<int>(col, 0)); queue<pair<int, int>> q; for (int i = 0; i < mid; i++) { int x = cells[i][0], y = cells[i][1]; table[x - 1][y - 1] = 1; } for (int i = 0; i < col; i++) { if (!table[0][i]) { q.push({0, i}); table[0][i] = -1; } } while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); if (x == row - 1) { return true; } for (pair<int, int> dir : d) { int tx = x + dir.first, ty = y + dir.second; if (tx >= 0 and tx < row and ty >= 0 and ty < col and table[tx][ty] == 0) { table[tx][ty] = -1; q.push(make_pair(tx, ty)); } } } return false; } int latestDayToCross(int row, int col, vector<vector<int>>& cells) { int left = 1; int right = row * col; while (left <= right) { int mid = (right + left) / 2; if (BFS(row, col, cells, mid)) left = mid + 1; else right = mid - 1; } return right; } };

Dark Theme License

The MIT License (MIT)

Copyright © 2022 Luminous-Coder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.