Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 06eab06

Browse files
authored
feat: add solutions to lc problem: No.0815 (doocs#3531)
1 parent 36e6eb2 commit 06eab06

File tree

8 files changed

+601
-494
lines changed

8 files changed

+601
-494
lines changed

solution/0800-0899/0815.Bus Routes/README.md

Lines changed: 220 additions & 150 deletions
Large diffs are not rendered by default.

solution/0800-0899/0815.Bus Routes/README_EN.md

Lines changed: 202 additions & 171 deletions
Large diffs are not rendered by default.

solution/0800-0899/0815.Bus Routes/Solution.cpp

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,45 @@ class Solution {
44
if (source == target) {
55
return 0;
66
}
7-
int n = routes.size();
8-
vector<unordered_set<int>> s(n);
9-
vector<vector<int>> g(n);
10-
unordered_map<int, vector<int>> d;
11-
for (int i = 0; i < n; ++i) {
12-
for (int v : routes[i]) {
13-
s[i].insert(v);
14-
d[v].push_back(i);
7+
8+
unordered_map<int, vector<int>> g;
9+
for (int i = 0; i < routes.size(); i++) {
10+
for (int stop : routes[i]) {
11+
g[stop].push_back(i);
1512
}
1613
}
17-
for (auto& [_, ids] : d) {
18-
int m = ids.size();
19-
for (int i = 0; i < m; ++i) {
20-
for (int j = i + 1; j < m; ++j) {
21-
int a = ids[i], b = ids[j];
22-
g[a].push_back(b);
23-
g[b].push_back(a);
24-
}
25-
}
26-
}
27-
queue<int> q;
28-
unordered_set<int> vis;
29-
int ans = 1;
30-
for (int v : d[source]) {
31-
q.push(v);
32-
vis.insert(v);
14+
15+
if (!g.contains(source) || !g.contains(target)) {
16+
return -1;
3317
}
18+
19+
queue<pair<int, int>> q;
20+
unordered_set<int> visBus;
21+
unordered_set<int> visStop;
22+
q.push({source, 0});
23+
visStop.insert(source);
24+
3425
while (!q.empty()) {
35-
for (int k = q.size(); k; --k) {
36-
int i = q.front();
37-
q.pop();
38-
if (s[i].count(target)) {
39-
return ans;
40-
}
41-
for (int j : g[i]) {
42-
if (!vis.count(j)) {
43-
vis.insert(j);
44-
q.push(j);
26+
auto [stop, busCount] = q.front();
27+
q.pop();
28+
29+
if (stop == target) {
30+
return busCount;
31+
}
32+
33+
for (int bus : g[stop]) {
34+
if (!visBus.contains(bus)) {
35+
for (int nextStop : routes[bus]) {
36+
if (!visStop.contains(nextStop)) {
37+
visBus.insert(bus);
38+
visStop.insert(nextStop);
39+
q.push({nextStop, busCount + 1});
40+
}
4541
}
4642
}
4743
}
48-
++ans;
4944
}
45+
5046
return -1;
5147
}
52-
};
48+
};

solution/0800-0899/0815.Bus Routes/Solution.cs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,39 @@ public int NumBusesToDestination(int[][] routes, int source, int target) {
44
return 0;
55
}
66

7-
Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
8-
List<HashSet<int>> routeToStops = new List<HashSet<int>>();
9-
7+
Dictionary<int, List<int>> g = new Dictionary<int, List<int>>();
108
for (int i = 0; i < routes.Length; i++) {
11-
routeToStops.Add(new HashSet<int>());
129
foreach (int stop in routes[i]) {
13-
routeToStops[i].Add(stop);
14-
if (!stopToRoutes.ContainsKey(stop)) {
15-
stopToRoutes[stop] = new HashSet<int>();
10+
if (!g.ContainsKey(stop)) {
11+
g[stop] = new List<int>();
1612
}
17-
stopToRoutes[stop].Add(i);
13+
g[stop].Add(i);
1814
}
1915
}
2016

21-
Queue<int> queue = new Queue<int>();
22-
HashSet<int> visited = new HashSet<int>();
23-
int ans = 0;
24-
25-
foreach (int routeId in stopToRoutes[source]) {
26-
queue.Enqueue(routeId);
27-
visited.Add(routeId);
17+
if (!g.ContainsKey(source) || !g.ContainsKey(target)) {
18+
return -1;
2819
}
2920

30-
while (queue.Count > 0) {
31-
int count = queue.Count;
32-
ans++;
33-
34-
for (int i = 0; i < count; i++) {
35-
int routeId = queue.Dequeue();
36-
37-
foreach (int stop in routeToStops[routeId]) {
38-
if (stop == target) {
39-
return ans;
40-
}
41-
42-
foreach (int nextRoute in stopToRoutes[stop]) {
43-
if (!visited.Contains(nextRoute)) {
44-
visited.Add(nextRoute);
45-
queue.Enqueue(nextRoute);
21+
Queue<int[]> q = new Queue<int[]>();
22+
HashSet<int> visBus = new HashSet<int>();
23+
HashSet<int> visStop = new HashSet<int>();
24+
q.Enqueue(new int[]{source, 0});
25+
visStop.Add(source);
26+
27+
while (q.Count > 0) {
28+
int[] current = q.Dequeue();
29+
int stop = current[0], busCount = current[1];
30+
if (stop == target) {
31+
return busCount;
32+
}
33+
foreach (int bus in g[stop]) {
34+
if (!visBus.Contains(bus)) {
35+
foreach (int nextStop in routes[bus]) {
36+
if (!visStop.Contains(nextStop)) {
37+
visBus.Add(bus);
38+
visStop.Add(nextStop);
39+
q.Enqueue(new int[]{nextStop, busCount + 1});
4640
}
4741
}
4842
}

solution/0800-0899/0815.Bus Routes/Solution.go

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,45 @@ func numBusesToDestination(routes [][]int, source int, target int) int {
22
if source == target {
33
return 0
44
}
5-
n := len(routes)
6-
s := make([]map[int]bool, n)
7-
g := make([][]int, n)
8-
d := map[int][]int{}
9-
for i, r := range routes {
10-
for _, v := range r {
11-
if s[i] == nil {
12-
s[i] = make(map[int]bool)
13-
}
14-
s[i][v] = true
15-
d[v] = append(d[v], i)
16-
}
17-
}
18-
for _, ids := range d {
19-
m := len(ids)
20-
for i := 0; i < m; i++ {
21-
for j := i + 1; j < m; j++ {
22-
a, b := ids[i], ids[j]
23-
g[a] = append(g[a], b)
24-
g[b] = append(g[b], a)
25-
}
5+
6+
g := make(map[int][]int)
7+
for i, route := range routes {
8+
for _, stop := range route {
9+
g[stop] = append(g[stop], i)
2610
}
2711
}
28-
q := d[source]
29-
vis := map[int]bool{}
30-
for _, v := range d[source] {
31-
vis[v] = true
12+
13+
if g[source] == nil || g[target] == nil {
14+
return -1
3215
}
33-
ans := 1
34-
for len(q) > 0 {
35-
for k := len(q); k > 0; k-- {
36-
i := q[0]
37-
q = q[1:]
38-
if s[i][target] {
39-
return ans
40-
}
41-
for _, j := range g[i] {
42-
if !vis[j] {
43-
vis[j] = true
44-
q = append(q, j)
16+
17+
q := list.New()
18+
q.PushBack([2]int{source, 0})
19+
visBus := make(map[int]bool)
20+
visStop := make(map[int]bool)
21+
visStop[source] = true
22+
23+
for q.Len() > 0 {
24+
front := q.Front()
25+
q.Remove(front)
26+
stop, busCount := front.Value.([2]int)[0], front.Value.([2]int)[1]
27+
28+
if stop == target {
29+
return busCount
30+
}
31+
32+
for _, bus := range g[stop] {
33+
if !visBus[bus] {
34+
visBus[bus] = true
35+
for _, nextStop := range routes[bus] {
36+
if !visStop[nextStop] {
37+
visStop[nextStop] = true
38+
q.PushBack([2]int{nextStop, busCount + 1})
39+
}
4540
}
4641
}
4742
}
48-
ans++
4943
}
44+
5045
return -1
51-
}
46+
}

solution/0800-0899/0815.Bus Routes/Solution.java

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,42 @@ public int numBusesToDestination(int[][] routes, int source, int target) {
33
if (source == target) {
44
return 0;
55
}
6-
int n = routes.length;
7-
Set<Integer>[] s = new Set[n];
8-
List<Integer>[] g = new List[n];
9-
Arrays.setAll(s, k -> new HashSet<>());
10-
Arrays.setAll(g, k -> new ArrayList<>());
11-
Map<Integer, List<Integer>> d = new HashMap<>();
12-
for (int i = 0; i < n; ++i) {
13-
for (int v : routes[i]) {
14-
s[i].add(v);
15-
d.computeIfAbsent(v, k -> new ArrayList<>()).add(i);
6+
7+
Map<Integer, List<Integer>> g = new HashMap<>();
8+
for (int i = 0; i < routes.length; i++) {
9+
for (int stop : routes[i]) {
10+
g.computeIfAbsent(stop, k -> new ArrayList<>()).add(i);
1611
}
1712
}
18-
for (var ids : d.values()) {
19-
int m = ids.size();
20-
for (int i = 0; i < m; ++i) {
21-
for (int j = i + 1; j < m; ++j) {
22-
int a = ids.get(i), b = ids.get(j);
23-
g[a].add(b);
24-
g[b].add(a);
25-
}
26-
}
27-
}
28-
Deque<Integer> q = new ArrayDeque<>();
29-
Set<Integer> vis = new HashSet<>();
30-
int ans = 1;
31-
for (int v : d.get(source)) {
32-
q.offer(v);
33-
vis.add(v);
13+
14+
if (!g.containsKey(source) || !g.containsKey(target)) {
15+
return -1;
3416
}
17+
18+
Deque<int[]> q = new ArrayDeque<>();
19+
Set<Integer> visBus = new HashSet<>();
20+
Set<Integer> visStop = new HashSet<>();
21+
q.offer(new int[] {source, 0});
22+
visStop.add(source);
23+
3524
while (!q.isEmpty()) {
36-
for (int k = q.size(); k > 0; --k) {
37-
int i = q.pollFirst();
38-
if (s[i].contains(target)) {
39-
return ans;
40-
}
41-
for (int j : g[i]) {
42-
if (!vis.contains(j)) {
43-
vis.add(j);
44-
q.offer(j);
25+
int[] current = q.poll();
26+
int stop = current[0], busCount = current[1];
27+
28+
if (stop == target) {
29+
return busCount;
30+
}
31+
for (int bus : g.get(stop)) {
32+
if (visBus.add(bus)) {
33+
for (int nextStop : routes[bus]) {
34+
if (visStop.add(nextStop)) {
35+
q.offer(new int[] {nextStop, busCount + 1});
36+
}
4537
}
4638
}
4739
}
48-
++ans;
4940
}
41+
5042
return -1;
5143
}
52-
}
44+
}

solution/0800-0899/0815.Bus Routes/Solution.py

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,23 @@ def numBusesToDestination(
44
) -> int:
55
if source == target:
66
return 0
7-
8-
# 一条公交线路有哪些公交站
9-
s = [set(r) for r in routes]
10-
11-
# 一个公交站在哪些公交线路有
12-
d = defaultdict(list)
13-
for i, r in enumerate(routes):
14-
for v in r:
15-
d[v].append(i)
16-
177
g = defaultdict(list)
18-
for ids in d.values():
19-
m = len(ids)
20-
for i in range(m):
21-
for j in range(i + 1, m):
22-
a, b = ids[i], ids[j]
23-
g[a].append(b)
24-
g[b].append(a)
25-
q = deque(d[source])
26-
ans = 1
27-
vis = set(d[source])
28-
while q:
29-
for _ in range(len(q)):
30-
i = q.popleft()
31-
if target in s[i]:
32-
return ans
33-
for j in g[i]:
34-
if j not in vis:
35-
vis.add(j)
36-
q.append(j)
37-
ans += 1
8+
for i, route in enumerate(routes):
9+
for stop in route:
10+
g[stop].append(i)
11+
if source not in g or target not in g:
12+
return -1
13+
q = [(source, 0)]
14+
vis_bus = set()
15+
vis_stop = {source}
16+
for stop, bus_count in q:
17+
if stop == target:
18+
return bus_count
19+
for bus in g[stop]:
20+
if bus not in vis_bus:
21+
vis_bus.add(bus)
22+
for next_stop in routes[bus]:
23+
if next_stop not in vis_stop:
24+
vis_stop.add(next_stop)
25+
q.append((next_stop, bus_count + 1))
3826
return -1

0 commit comments

Comments
 (0)