ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

leetcode 407 接雨水

2022-03-28 01:02:35  阅读:226  来源: 互联网

标签:heightMap current list 雨水 height 407 num line leetcode


三维空间的接雨水
有一点像dijkstar最短路径搜索。
所谓dijkstar, 是有一个closeset的,closeset表明的是已经确定距离的位置。
初始位置是closeset,每次都取与closeset相邻的最近的点放入到closet中。 相邻的点用一个最小堆来维护。

这道题也是一样, 初始的closeset是周围一圈,因为周围一圈是不会存雨水的。 然后取其中最小的值,该点为a点, 与最小的值相邻的某个点b的雨水高度可以由a,b中的高度最大值获得, 此时b点的接完雨水的高度也已经确定, 可以放入的close队列中去。

以上

class Solution:
    def trapRainWater(self, heightMap: List[List[int]]) -> int:
        if not heightMap or not heightMap[0]:
            return 0
        line_num = len(heightMap)
        col_num = len(heightMap[0])
        if line_num <= 2 or col_num <=2:
            return 0
        result = 0
        open_sign_list = [[0 for _ in range(col_num)] for _ in range(line_num)]

        height_list = []
        for j in range(col_num):
            heapq.heappush(height_list, (heightMap[0][j], 0, j))
            heapq.heappush(height_list, (heightMap[line_num - 1][j], line_num - 1, j))
            open_sign_list[0][j] = 1
            open_sign_list[line_num - 1][j] = 1

        for i in range(1, line_num - 1):
            heapq.heappush(height_list, (heightMap[i][0], i, 0))
            heapq.heappush(height_list, (heightMap[i][col_num - 1], i, col_num - 1))
            open_sign_list[i][0] = 1
            open_sign_list[i][col_num - 1] = 1

        while height_list:
            height, i, j = heapq.heappop(height_list)
            for x, y in [(i - 1, j), (i + 1, j), (i, j -1), (i, j + 1)]:
                if x > line_num - 1 or x < 0 or y > col_num - 1 or y < 0:
                    continue
                elif open_sign_list[x][y] == 1:
                    continue
                else:
                    current_height = heightMap[x][y]
                    final_height = max(current_height, height)
                    heapq.heappush(height_list, (final_height, x, y))
                    result += final_height - current_height
                    open_sign_list[x][y] = 1

        return result

上述的方法在遇到相邻的点依旧是最低点的时候会入堆出堆一次, 所以简单再优化一下

class Solution:
    def trapRainWater(self, heightMap: List[List[int]]) -> int:
        if not heightMap or not heightMap[0]:
            return 0
        line_num = len(heightMap)
        col_num = len(heightMap[0])
        if line_num <= 2 or col_num <=2:
            return 0
        result = 0
        open_sign_list = [[0 for _ in range(col_num)] for _ in range(line_num)]

        height_list = []
        for j in range(col_num):
            heapq.heappush(height_list, (heightMap[0][j], 0, j))
            heapq.heappush(height_list, (heightMap[line_num - 1][j], line_num - 1, j))
            open_sign_list[0][j] = 1
            open_sign_list[line_num - 1][j] = 1

        for i in range(1, line_num - 1):
            heapq.heappush(height_list, (heightMap[i][0], i, 0))
            heapq.heappush(height_list, (heightMap[i][col_num - 1], i, col_num - 1))
            open_sign_list[i][0] = 1
            open_sign_list[i][col_num - 1] = 1

        # 我这里的思路就是如果相邻的点依旧是最小点就无需入堆再出堆了
        tmp_list = []
        while tmp_list or height_list:
            if tmp_list:
                height, i, j = tmp_list.pop()
            else:
                height, i, j = heapq.heappop(height_list)

            for x, y in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
                if x > line_num - 1 or x < 0 or y > col_num - 1 or y < 0:
                    continue
                elif open_sign_list[x][y] == 1:
                    continue
                else:
                    current_height = heightMap[x][y]
                    if current_height > height:
                        final_height = current_height
                        heapq.heappush(height_list, (final_height, x, y))
                        open_sign_list[x][y] = 1
                    else:
                        open_sign_list[x][y] = 1
                        tmp_list.append((height, x, y))
                        result += height - current_height
        return result

以上

标签:heightMap,current,list,雨水,height,407,num,line,leetcode
来源: https://www.cnblogs.com/mangmangbiluo/p/16065354.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有