LeetCode題解(0407):接雨水II(Python)
題目:原題連結(困難)
標籤:堆、廣度優先搜尋
解法 | 時間複雜度 | 空間複雜度 | 執行用時 |
---|---|---|---|
Ans 1 (Python) | O ( N M l o g ( N + M ) ) O(NMlog(N+M)) O(NMlog(N+M)) | O ( N + M ) O(N+M) O(N+M) | 144ms (80.58%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一(海平面上升思路):
class Solution:
def trapRainWater(self, heightMap: List[List[int]]) -> int:
# 判斷位置是否有效
def is_valid(x, y):
return 0 <= x < s1 and 0 <= y < s2
# 海平面不斷升高淹沒盆地思路
s1, s2 = len(heightMap), len(heightMap[0])
s = s1 * s2
# 處理池子過小的情況
# O(1)
if s1 <= 2 or s2 <= 2:
return 0
# 計算最外層堤壩位置
# O(M+N)
wall_list = []
for i1 in range(0, s1):
wall_list.append((i1, 0))
wall_list.append((i1, s2 - 1))
for i2 in range(0, s2):
wall_list.append((0, i2))
wall_list.append((s1 - 1, i2))
# 處理最外層堤壩高度
visited = set()
heap = []
for i1, i2 in wall_list:
heap.append((heightMap[i1][i2], i1, i2))
visited.add((i1, i2))
# 使最外層堤壩高度具有堆的特徵
heapq.heapify(heap)
# 初始化海平面高度
now_height = 0
# 開始提升海平面高度直至只剩下狹長(中間不包括被堤壩包圍的陸地)島嶼
ans = 0
while len(visited) < s:
# 從堤壩高度堆中取出最低的堤壩
height, i1, i2 = heapq.heappop(heap)
# 升高(或不用升高)海平面至可以淹沒堤壩的高度
now_height = max(now_height, height)
# 計算堤壩周圍的座標位置
neighbours = [(i1 + 1, i2), (i1 - 1, i2), (i1, i2 + 1), (i1, i2 - 1)]
# 計算堤壩周圍的非堤壩或海洋位置(腹地位置)
inners = [neighbour for neighbour in neighbours if
(is_valid(neighbour[0], neighbour[1]) and neighbour not in visited)]
# 檢查腹地位置的淹沒情況
for ii1, ii2 in inners:
# 如果腹地位置比當前海平面低,則可以增加積水量
if heightMap[ii1][ii2] < now_height:
ans += now_height - heightMap[ii1][ii2]
# 將當前腹地位置作為新的堤壩
heapq.heappush(heap, (heightMap[ii1][ii2], ii1, ii2))
visited.add((ii1, ii2))
return ans
相關文章
- 每日leetcode——42. 接雨水LeetCode
- [Python手撕]接雨水Python
- LeetCode題解(0210):課程表II(Python)LeetCodePython
- 【完虐演算法】LeetCode 接雨水問題,全覆盤演算法LeetCode
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- 單調棧進階-接雨水-最大矩形
- 【leetcode】每日精選題詳解之59. 螺旋矩陣 IILeetCode矩陣
- P8475 「GLR-R3」雨水 題解
- LeetCode 59. 螺旋矩陣 II(python、c++)LeetCode矩陣PythonC++
- leetcode 831題解【C++/Java/Python】LeetCodeC++JavaPython
- LeetCode題解(1652):拆炸彈(Python)LeetCodePython
- LeetCode題解(1178):猜字謎(Python)LeetCodePython
- leetcode 解題 2.兩數相加-python3 題解LeetCodePython
- Leetcode每日一題:52.N-Queens II(N皇后Ⅱ)LeetCode每日一題
- 每日一道演算法題--leetcode 113--路徑總和II--python演算法LeetCodePython
- leetcode-90. Subsets IILeetCode
- Leetcode 40 Combination Sum IILeetCode
- Leetcode 213 House Robber IILeetCode
- LeetCode 1103[分糖果II]LeetCode
- LeetCode題解:264. 醜數 II,二叉堆,JavaScript,詳細註釋LeetCodeJavaScript
- LeetCode題解(1668):最大重複子字串(Python)LeetCode字串Python
- leetcode 題解:python3@ 官方題解_暴力法_雙指標法LeetCodePython指標
- LeetCode每日一題: 按奇偶排序陣列 II(No.27)LeetCode每日一題排序陣列
- [LeetCode] 210. Course Schedule IILeetCode
- [LeetCode] 305. Number of Islands IILeetCode
- [LeetCode] 212. Word Search IILeetCode
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- [LeetCode] 2105. Watering Plants IILeetCode
- LeetCode 52. N皇后 IILeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- leetcode 219. Contains Duplicate IILeetCodeAI
- [LeetCode] 910. Smallest Range IILeetCode
- [LeetCode] 45. Jump Game IILeetCodeGAM
- LeetCode-047-全排列 IILeetCode
- Leetcode 137. Single Number IILeetCode
- [LeetCode] 3152. Special Array IILeetCode
- leetcode 11 題解:python3@ 官方題解_暴力法_雙指標法LeetCodePython指標