【力扣399. 除法求值】有权重并查集(Python3)

题目描述

https://leetcode-cn.com/problems/evaluate-division/

思路题解

这道题差点没把我送走
https://leetcode-cn.com/problems/evaluate-division/solution/399-chu-fa-qiu-zhi-nan-du-zhong-deng-286-w45d/
还要考虑一种情况
在这里插入图片描述
合并的时候按照四边形法则
代码如下:

class Solution:
    class UnionFind:
        def __init__(self):
            self.parent={}
            self.weight={}
        def find(self,index):
            if index not in self.parent:
                self.parent[index]=index
                self.weight[index]=1.0
                return index
        	# 自己就是根节点
            if self.parent[index]==index:
                return index
            # 查找根节点+路径压缩
            origin_p=self.parent[index]
            self.parent[index]=self.find(self.parent[index])
            self.weight[index]*=self.weight[origin_p]
            return self.parent[index]
        def union(self,index1,index2,w):
            p1,p2=self.parent[index1],self.parent[index2]
            self.parent[p1]=p2
            self.weight[p1]=w*self.weight[index2]/self.weight[index1]
    def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
        uf=Solution.UnionFind()
        ans=[]
        for i in range(len(equations)):
            if uf.find(equations[i][0])!=uf.find(equations[i][1]):
                uf.union(equations[i][0],equations[i][1],values[i])
        for i in range(len(queries)):
            if queries[i][0] not in uf.weight or queries[i][1] not in uf.weight or uf.find(queries[i][0])!=uf.find(queries[i][1]):
                ans.append(-1.0)
            else:
                ans.append(uf.weight[queries[i][0]]/uf.weight[queries[i][1]])
        return ans

在这里插入图片描述
对红框部分的路径压缩进行优化,减少递归次数
在这里插入图片描述

class Solution:
    class UnionFind:
        def __init__(self):
            self.parent={}
            self.weight={}
        def find(self,index):
            if index not in self.parent:
                self.parent[index]=index
                self.weight[index]=1.0
                return index
            root=index #root代表根
            base=1.0 #base代表路径上(index,root]所有权重的乘积
            while self.parent[root]!=root:
                root=self.parent[root]
                base*=self.weight[root]#index节点不乘进去
            # 查找根节点+路径压缩 
            while index!=root:
                self.weight[index]*=base
                origin_p=self.parent[index]
                base/=self.weight[origin_p]
                self.parent[index]=root
                index=origin_p
            
            return self.parent[index]
        def union(self,index1,index2,w):
            p1,p2=self.parent[index1],self.parent[index2]
            self.parent[p1]=p2
            self.weight[p1]=w*self.weight[index2]/self.weight[index1]
    def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
        uf=Solution.UnionFind()
        ans=[]
        for i in range(len(equations)):
            if uf.find(equations[i][0])!=uf.find(equations[i][1]):
                uf.union(equations[i][0],equations[i][1],values[i])
        for i in range(len(queries)):
            if queries[i][0] not in uf.weight or queries[i][1] not in uf.weight or uf.find(queries[i][0])!=uf.find(queries[i][1]):
                ans.append(-1.0)
            else:
                ans.append(uf.weight[queries[i][0]]/uf.weight[queries[i][1]])
        return ans

在这里插入图片描述
并查集总结:
https://blog.csdn.net/anan15151529/article/details/118416802

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值