【菜鸡刷题-leetcode778 】水位上升的泳池中游泳 |并查集 | 字典 | python


@author = YHR | 转载请标明来源!



题目描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


题解

先马一下重点吧,这几天真的好累,来不及更博,刷题强度也有点大,大概马一下几个关键的地方

并查集 – 连通问题

官方给我最大的帮助就是

  1. 并查集 不需要按结点的顺序来连通,任何俩可以连通就连通!

然后出现了时间限制

  1. 时间就对应threshhold,等于threshold的位置,在此刻海拔终于等于0!has been drowned

  2. 然后再看同学们的解法 给我的启发就是:已经连通的人,就别搭理他了,决定权都在没连通的人的手上! 缩小范围了~

就缩小了时间,过了!


python 代码

'''
 
官方给我最大的帮助就是
 
1. 并查集 不需要按结点的顺序来连通,任何俩可以连通就连通!
 
然后出现了时间限制

2. 时间就对应threshhold,等于threshold的位置,在此刻海拔终于等于0!has been drowned
 
3. 然后再看同学们的解法 给我的启发就是
 
已经连通的人,就别搭理他了,决定权都在没连通的人的手上! 缩小范围了~
 
就缩小了时间,过了!


'''
class Solution:
    def swimInWater(self, grid) :
        self.rows = len(grid)
        self.cols = len(grid[0])
        return (self.method(grid))
        pass

    def useIdgetRowCol(self, id):
        row = id//self.cols
        col = id%self.cols
        return row, col

    def findParent(self, parentDict, node):
        parent = parentDict[node]

        while parent != node:
            node = parent
            parent = parentDict[node]
        return parent



    def method(self, grid):
        # initial the heightHash
        heightsHash = {}
        for _r in range(self.rows):
            for _c in range(self.cols):
                val = grid[_r][_c]
                if val in heightsHash:
                    heightsHash[val].append(_r * self.cols + _c)
                else:
                    heightsHash[val] = [_r * self.cols + _c]


        parentDict = [_ for _ in range(self.rows*self.cols)]
        childsize = [1]*(self.rows*self.cols)
        
        dir = [[0,-1], [0,1], [1,0], [-1,0]]
        t =  0
        # key= 0 表示当前第t时刻这些index的位置已经被水淹了!
        new_drowned_indexes = heightsHash[0]
        while t>=0:
            # new_drowned_indexes -- 已经连通的人,就别搭理他了,决定权都在没连通的人的手上! 缩小范围了~
            for drowned_id in new_drowned_indexes:
                this_r, this_c = self.useIdgetRowCol(drowned_id)
                this_parent = self.findParent(parentDict, drowned_id)
                for _ in range(4):
                    new_r, new_c = this_r+dir[_][0], this_c+dir[_][1]
                    if 0<=new_r<self.rows and 0<=new_c<self.cols and (new_r*self.cols+new_c) in heightsHash[0]:
                        new_parent = self.findParent(parentDict, (new_r*self.cols+new_c))
                        if childsize[this_parent] < childsize[new_parent]:
                            this_parent, new_parent = new_parent , this_parent
                        parentDict[new_parent] = this_parent
                        childsize[this_parent] += childsize[new_parent]

            if self.findParent(parentDict, 0) == self.findParent(parentDict, self.rows*self.cols-1):
                return t
            
            t += 1  # t增加一秒
            # 更新相对的heights
            if t>0:
                if t in heightsHash:
                    heightsHash[0] += heightsHash[t]
                    new_drowned_indexes = heightsHash[t]  # 新加进来的unseen -- new_drowned_indexes 还没连通的人的手上
                    del heightsHash[t]

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值