| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.PathWithMaximumProbability; |
| | | 13 | | |
| | | 14 | | public abstract class PathWithMaximumProbabilityBase : IPathWithMaximumProbability |
| | | 15 | | { |
| | | 16 | | public abstract double MaxProbability(int n, int[][] edges, double[] successProbability, int startNode, |
| | | 17 | | int endNode); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time Complexity - O(n), where n is the number of edges |
| | | 21 | | /// Time Complexity - O(n + m), where n is the number of edges and m is the number of edges |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="edges"></param> |
| | | 24 | | /// <param name="successProbability"></param> |
| | | 25 | | /// <returns></returns> |
| | | 26 | | protected static Dictionary<int, List<(int Node, double Probability)>> GetEdgesDictionary(int[][] edges, |
| | | 27 | | double[] successProbability) |
| | 14 | 28 | | { |
| | 14 | 29 | | var edgesDictionary = new Dictionary<int, List<(int Node, double Probability)>>(); |
| | | 30 | | |
| | 196 | 31 | | for (var i = 0; i < edges.Length; i++) |
| | 84 | 32 | | { |
| | 84 | 33 | | if (edgesDictionary.TryGetValue(edges[i][0], out var headEdge)) |
| | 66 | 34 | | { |
| | 66 | 35 | | headEdge.Add((edges[i][1], successProbability[i])); |
| | 66 | 36 | | } |
| | | 37 | | else |
| | 18 | 38 | | { |
| | 18 | 39 | | edgesDictionary[edges[i][0]] = [(edges[i][1], successProbability[i])]; |
| | 18 | 40 | | } |
| | | 41 | | |
| | 84 | 42 | | if (edgesDictionary.TryGetValue(edges[i][1], out var tailEdge)) |
| | 18 | 43 | | { |
| | 18 | 44 | | tailEdge.Add((edges[i][0], successProbability[i])); |
| | 18 | 45 | | } |
| | | 46 | | else |
| | 66 | 47 | | { |
| | 66 | 48 | | edgesDictionary[edges[i][1]] = [(edges[i][0], successProbability[i])]; |
| | 66 | 49 | | } |
| | 84 | 50 | | } |
| | | 51 | | |
| | 14 | 52 | | return edgesDictionary; |
| | 14 | 53 | | } |
| | | 54 | | } |