< Summary

Information
Class: LeetCode.Algorithms.CheapestFlightsWithinKStops.CheapestFlightsWithinKStopsBellmanFord
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CheapestFlightsWithinKStops\CheapestFlightsWithinKStopsBellmanFord.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 63
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindCheapestPrice(...)90%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CheapestFlightsWithinKStops\CheapestFlightsWithinKStopsBellmanFord.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 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
 12namespace LeetCode.Algorithms.CheapestFlightsWithinKStops;
 13
 14/// <inheritdoc />
 15public class CheapestFlightsWithinKStopsBellmanFord : ICheapestFlightsWithinKStops
 16{
 17    /// <summary>
 18    ///     Time complexity - O (k * E), where k is the number of stops allowed and E is the number of edges(flights).
 19    ///     Space complexity - O (n)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <param name="flights"></param>
 23    /// <param name="src"></param>
 24    /// <param name="dst"></param>
 25    /// <param name="k"></param>
 26    /// <returns></returns>
 27    public int FindCheapestPrice(int n, int[][] flights, int src, int dst, int k)
 428    {
 429        var cost = new int[n];
 30
 431        Array.Fill(cost, int.MaxValue);
 32
 433        cost[src] = 0;
 34
 435        var temp = new int[n];
 36
 437        Array.Copy(cost, temp, n);
 38
 2239        for (var i = 0; i <= k; i++)
 740        {
 741            Array.Copy(cost, temp, n);
 42
 7143            foreach (var flight in flights)
 2544            {
 7545                int u = flight[0], v = flight[1], w = flight[2];
 46
 2547                if (cost[u] == int.MaxValue)
 1148                {
 1149                    continue;
 50                }
 51
 1452                if (cost[u] + w < temp[v])
 1053                {
 1054                    temp[v] = cost[u] + w;
 1055                }
 1456            }
 57
 758            Array.Copy(temp, cost, n);
 759        }
 60
 461        return cost[dst] == int.MaxValue ? -1 : cost[dst];
 462    }
 63}