Skip to content

Commit 475756a

Browse files
committed
一刷787
1 parent 476027d commit 475756a

File tree

9 files changed

+168
-45
lines changed

9 files changed

+168
-45
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5534,14 +5534,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
55345534
//|{doc_base_url}/0786-k-th-smallest-prime-fraction.adoc[题解]
55355535
//|Hard
55365536
//|
5537-
//
5538-
//|{counter:codes}
5539-
//|{leetcode_base_url}/cheapest-flights-within-k-stops/[787. Cheapest Flights Within K Stops^]
5540-
//|{source_base_url}/_0787_CheapestFlightsWithinKStops.java[Java]
5541-
//|{doc_base_url}/0787-cheapest-flights-within-k-stops.adoc[题解]
5542-
//|Medium
5543-
//|
5544-
//
5537+
5538+
|{counter:codes}
5539+
|{leetcode_base_url}/cheapest-flights-within-k-stops/[787. Cheapest Flights Within K Stops^]
5540+
|{source_base_url}/_0787_CheapestFlightsWithinKStops.java[Java]
5541+
|{doc_base_url}/0787-cheapest-flights-within-k-stops.adoc[题解]
5542+
|Medium
5543+
|
5544+
55455545
//|{counter:codes}
55465546
//|{leetcode_base_url}/rotated-digits/[788. Rotated Digits^]
55475547
//|{source_base_url}/_0788_RotatedDigits.java[Java]
Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,99 @@
11
[#0787-cheapest-flights-within-k-stops]
2-
= 787. Cheapest Flights Within K Stops
2+
= 787. K 站中转内最便宜的航班
33

4-
{leetcode}/problems/cheapest-flights-within-k-stops/[LeetCode - Cheapest Flights Within K Stops^]
4+
https://leetcode.cn/problems/cheapest-flights-within-k-stops/[LeetCode - 787. K 站中转内最便宜的航班 ^]
55

6-
There are `n` cities connected by `m` flights. Each fight starts from city `u `and arrives at `v` with a price `w`.
6+
`n` 个城市通过一些航班连接。给你一个数组 `flights`,其中 `flights[i] = [from~i~, to~i~, price~i~]`,表示该航班都从城市 `from~i~ 开始,以价格 `price~i~ 抵达 `to~i~。
77

8-
Now given all the cities and flights, together with starting city `src` and the destination `dst`, your task is to find the cheapest price from `src` to `dst` with up to `k` stops. If there is no such route, output `-1`.
8+
现在给定所有的城市和航班,以及出发城市 `src` 和目的地 `dst`,你的任务是找到出一条最多经过 `k` 站中转的路线,使得从 `src` `dst` *价格最便宜* ,并返回该价格。 如果不存在这样的路线,则输出 `-1`
99

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
*Example 1:*
13-
*Input:*
14-
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
15-
src = 0, dst = 2, k = 1
16-
*Output:* 200
17-
*Explanation:*
18-
The graph looks like this:
1910

20-
image::https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png[{image_attr}]
11+
*示例 1:*
2112

22-
The cheapest price from city `0` to city `2` with at most 1 stop costs 200, as marked red in the picture.
23-
----
13+
image::images/0787-01.png[{image_attr}]
2414

25-
[subs="verbatim,quotes,macros"]
26-
----
27-
*Example 2:*
28-
*Input:*
29-
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
30-
src = 0, dst = 2, k = 0
31-
*Output:* 500
32-
*Explanation:*
33-
The graph looks like this:
15+
....
16+
输入:
17+
n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
18+
输出: 700
19+
解释: 城市航班图如上
20+
从城市 0 到城市 3 经过最多 1 站的最佳路径用红色标记,费用为 100 + 600 = 700。
21+
请注意,通过城市 [0, 1, 2, 3] 的路径更便宜,但无效,因为它经过了 2 站。
22+
....
3423

35-
image::https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png[{image_attr}]
24+
*示例 2:*
3625

37-
The cheapest price from city `0` to city `2` with at most 0 stop costs 500, as marked blue in the picture.
38-
----
26+
image::images/0787-02.png[{image_attr}]
3927

40-
*Note:*
28+
....
29+
输入:
30+
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
31+
输出: 200
32+
解释:
33+
城市航班图如上
34+
从城市 0 到城市 2 经过最多 1 站的最佳路径标记为红色,费用为 100 + 100 = 200。
35+
....
4136

37+
*示例 3:*
4238

43-
* The number of nodes `n` will be in range `[1, 100]`, with nodes labeled from `0` to `n`` - 1`.
44-
* The size of `flights` will be in range `[0, n * (n - 1) / 2]`.
45-
* The format of each flight will be `(src, ``dst``, price)`.
46-
* The price of each flight will be in the range `[1, 10000]`.
47-
* `k` is in the range of `[0, n - 1]`.
48-
* There will not be any duplicated flights or self cycles.
39+
image::images/0787-03.png[{image_attr}]
4940

41+
....
42+
输入:n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
43+
输出:500
44+
解释:
45+
城市航班图如上
46+
从城市 0 到城市 2 不经过站点的最佳路径标记为红色,费用为 500。
47+
....
5048

49+
*提示:*
50+
51+
* `+1 <= n <= 100+`
52+
* `+0 <= flights.length <= (n * (n - 1) / 2)+`
53+
* `flights[i].length == 3`
54+
* `0 \<= from~i~, to~i~ < n`
55+
* `from~i~ != to~i~`
56+
* `1 \<= price~i~ \<= 10^4^`
57+
* 航班没有重复,且不存在自环
58+
* `+0 <= src, dst, k < n+`
59+
* `src != dst`
60+
61+
62+
== 思路分析
5163

5264

5365
[[src-0787]]
66+
[tabs]
67+
====
68+
一刷(回溯,超时)::
69+
+
70+
--
5471
[{java_src_attr}]
5572
----
5673
include::{sourcedir}/_0787_CheapestFlightsWithinKStops.java[tag=answer]
5774
----
75+
--
76+
77+
一刷(动态规划)::
78+
+
79+
--
80+
[{java_src_attr}]
81+
----
82+
include::{sourcedir}/_0787_CheapestFlightsWithinKStops_1.java[tag=answer]
83+
----
84+
--
85+
86+
// 二刷::
87+
// +
88+
// --
89+
// [{java_src_attr}]
90+
// ----
91+
// include::{sourcedir}/_0787_CheapestFlightsWithinKStops_2.java[tag=answer]
92+
// ----
93+
// --
94+
====
95+
96+
97+
== 参考资料
98+
5899

docs/images/0787-01.png

22.8 KB
Loading

docs/images/0787-02.png

14.8 KB
Loading

docs/images/0787-03.png

14.9 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ include::0768-max-chunks-to-make-sorted-ii.adoc[leveloffset=+1]
16581658

16591659
// include::0786-k-th-smallest-prime-fraction.adoc[leveloffset=+1]
16601660

1661-
// include::0787-cheapest-flights-within-k-stops.adoc[leveloffset=+1]
1661+
include::0787-cheapest-flights-within-k-stops.adoc[leveloffset=+1]
16621662

16631663
// include::0788-rotated-digits.adoc[leveloffset=+1]
16641664

logbook/202503.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,16 @@ endif::[]
609609
|✅ 深度优先遍历。按照 `<level, node>` 的格式,把每个节点都放到 `Map` 里,因为是先左后右,所以,每层最后只剩下了最右边的元素。
610610

611611
|{counter:codes2503}
612-
|{leetcode_base_url}/maximum-number-of-moves-in-a-grid/[2684. Maximum Number of Moves in a Grid^]
612+
|{leetcode_base_url}/maximum-number-of-moves-in-a-grid/[2684. 矩阵中移动的最大次数^]
613613
|{doc_base_url}/2684-maximum-number-of-moves-in-a-grid.adoc[题解]
614614
|✅ 动态规划。也可以使用深度优先搜索的解法,使用深度优先搜索时,需要把处理过的节点值设置为 `0`,防止重新判断,加快处理速度。
615615

616+
|{counter:codes2503}
617+
|{leetcode_base_url}/cheapest-flights-within-k-stops/[787. K 站中转内最便宜的航班^]
618+
|{doc_base_url}/0787-cheapest-flights-within-k-stops.adoc[题解]
619+
|⭕️ 回溯解法通过 48 / 56 个测试用例。
620+
621+
616622
|===
617623

618624
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.*;
4+
5+
public class _0787_CheapestFlightsWithinKStops {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-05-06 18:53:25
10+
*/
11+
int result = Integer.MAX_VALUE;
12+
13+
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
14+
Map<Integer, List<int[]>> srcMap = new HashMap<>();
15+
for (int[] flight : flights) {
16+
int start = flight[0];
17+
srcMap.computeIfAbsent(start, x -> new ArrayList<>()).add(flight);
18+
}
19+
backtrack(n, srcMap, src, dst, k + 1, 0);
20+
return result == Integer.MAX_VALUE ? -1 : result;
21+
}
22+
23+
private void backtrack(int n, Map<Integer, List<int[]>> srcMap,
24+
int src, int dst, int k, int expend) {
25+
26+
if (k < 0) {
27+
return;
28+
}
29+
if (src == dst) {
30+
result = Math.min(result, expend);
31+
return;
32+
}
33+
for (int[] flight : srcMap.getOrDefault(src, Collections.emptyList())) {
34+
int idst = flight[1];
35+
int cost = flight[2];
36+
// 不加该判断,通过 28 / 56 个测试用例;加上,通过 48 / 56 个测试用例
37+
if (cost + expend >= result) {
38+
continue;
39+
}
40+
backtrack(n, srcMap, idst, dst, k - 1, cost + expend);
41+
}
42+
}
43+
// end::answer[]
44+
public static void main(String[] args) {
45+
new _0787_CheapestFlightsWithinKStops().findCheapestPrice(4,
46+
new int[][]{
47+
{0, 1, 100},
48+
{1, 2, 100},
49+
{2, 0, 100},
50+
{1, 3, 600},
51+
{2, 3, 200}}, 0, 3, 1);
52+
}
53+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0787_CheapestFlightsWithinKStops_1 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-06 19:30:49
9+
*/
10+
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
11+
return 0; // TODO
12+
}
13+
// end::answer[]
14+
public static void main(String[] args) {
15+
new _0787_CheapestFlightsWithinKStops_1().findCheapestPrice(4,
16+
new int[][]{
17+
{0, 1, 100},
18+
{1, 2, 100},
19+
{2, 0, 100},
20+
{1, 3, 600},
21+
{2, 3, 200}}, 0, 3, 1);
22+
}
23+
}

0 commit comments

Comments
 (0)