Re: [閒聊] 每日leetcode

看板Marginalman作者 (通通打死)時間1年前 (2024/08/28 00:02), 編輯推噓1(101)
留言2則, 2人參與, 1年前最新討論串773/1550 (看更多)
乘一堆負號好麻煩 自討苦吃 但我也懶得去找怎麼讓heap是maxheap了 對不起 def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int) -> float: g = defaultdict(list) for i in range(len(edges)): g[edges[i][0]].append((edges[i][1],succProb[i])) g[edges[i][1]].append((edges[i][0],succProb[i])) # dijkstra probes = [0.0 for _ in range(n)] probes[start_node] = 1.0 pq = [(-1.0, start_node)] while pq: cur_prob, cur_node = heappop(pq) if -1.0*cur_prob < probes[cur_node]: continue if cur_node == end_node: return -1.0*cur_prob for n, p in g[cur_node]: if cur_prob*p < -1.0*probes[n]: probes[n] = -1.0*cur_prob*p heappush(pq, (cur_prob*p, n)) return 0.0 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1724774543.A.C3F.html

08/28 00:02, 1年前 , 1F
別再卷了......
08/28 00:02, 1F

08/28 00:03, 1年前 , 2F
只能乘負號 py大便heap
08/28 00:03, 2F
文章代碼(AID): #1cpVYFm_ (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cpVYFm_ (Marginalman)