I am trying to solve this problem:
Path with Maximum Probability leetcode 1514
You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i].
Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.
If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.
My code pass the test case but when I submit, for large data, it is giving wrong answer for one test case with very large node. I am doubting that problem lies in memo but can’t think of what might be the issue,
var maxProbability = function (n, edges, succProb, start_node, end_node) {
const graph = {}
for (let i = 0; i < edges.length; i++) {
const [a, b] = edges[i]
if (!graph[a]) graph[a] = {}
if (!graph[b]) graph[b] = {}
graph[a][b] = succProb[i]
graph[b][a] = succProb[i]
}
const probability = (current_node, end_node, memo = {}, visited = {})=>{
if(current_node == end_node) return 1
visited[current_node] = true
if(memo[current_node]) return memo[current_node]
let result = 0;
for(const node in graph[current_node]){
if(!visited[node]){
result = Math.max(graph[current_node][node] * probability(node, end_node, memo, visited), result)
}
}
delete visited[current_node]
memo[current_node] = result
return memo[current_node]
}
return probability(start_node, end_node)
};
can anyone please help me understand what I am doing wrong.