Class Solution
java.lang.Object
g0701_0800.s0787_cheapest_flights_within_k_stops.Solution
787 - Cheapest Flights Within K Stops.<p>Medium</p>
<p>There are <code>n</code> cities connected by some number of flights. You are given an array <code>flights</code> where <code>flights[i] = [from<sub>i</sub>, to<sub>i</sub>, price<sub>i</sub>]</code> indicates that there is a flight from city <code>from<sub>i</sub></code> to city <code>to<sub>i</sub></code> with cost <code>price<sub>i</sub></code>.</p>
<p>You are also given three integers <code>src</code>, <code>dst</code>, and <code>k</code>, return <em><strong>the cheapest price</strong> from</em> <code>src</code> <em>to</em> <code>dst</code> <em>with at most</em> <code>k</code> <em>stops.</em> If there is no such route, return <code>-1</code>.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png" alt="" /></p>
<p><strong>Input:</strong> n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1</p>
<p><strong>Output:</strong> 200</p>
<p><strong>Explanation:</strong></p>
<p>The graph is shown.</p>
<p>The cheapest price from city <code>0</code> to city <code>2</code> with at most 1 stop costs 200, as marked red in the picture.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png" alt="" /></p>
<p><strong>Input:</strong> n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0</p>
<p><strong>Output:</strong> 500</p>
<p><strong>Explanation:</strong></p>
<p>The graph is shown.</p>
<p>The cheapest price from city <code>0</code> to city <code>2</code> with at most 0 stop costs 500, as marked blue in the picture.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>0 <= flights.length <= (n * (n - 1) / 2)</code></li>
<li><code>flights[i].length == 3</code></li>
<li><code>0 <= from<sub>i</sub>, to<sub>i</sub> < n</code></li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
<li><code>1 <= price<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>There will not be any multiple flights between two cities.</li>
<li><code>0 <= src, dst, k < n</code></li>
<li><code>src != dst</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
findCheapestPrice
(int n, int[][] flights, int src, int dst, int k)
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
findCheapestPrice
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k)
-