Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cs solution to lc problem: No.815 #1957

Merged
merged 4 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions solution/0800-0899/0815.Bus Routes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,65 @@ func numBusesToDestination(routes [][]int, source int, target int) int {
}
```

### **C#**

```cs
public class Solution {
public int NumBusesToDestination(int[][] routes, int source, int target) {
if (source == target) {
return 0;
}

Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
List<HashSet<int>> routeToStops = new List<HashSet<int>>();

for (int i = 0; i < routes.Length; i++) {
routeToStops.Add(new HashSet<int>());
foreach (int stop in routes[i]) {
routeToStops[i].Add(stop);
if (!stopToRoutes.ContainsKey(stop)) {
stopToRoutes[stop] = new HashSet<int>();
}
stopToRoutes[stop].Add(i);
}
}

Queue<int> queue = new Queue<int>();
HashSet<int> visited = new HashSet<int>();
int ans = 0;

foreach (int routeId in stopToRoutes[source]) {
queue.Enqueue(routeId);
visited.Add(routeId);
}

while (queue.Count > 0) {
int count = queue.Count;
ans++;

for (int i = 0; i < count; i++) {
int routeId = queue.Dequeue();

foreach (int stop in routeToStops[routeId]) {
if (stop == target) {
return ans;
}

foreach (int nextRoute in stopToRoutes[stop]) {
if (!visited.Contains(nextRoute)) {
visited.Add(nextRoute);
queue.Enqueue(nextRoute);
}
}
}
}
}

return -1;
}
}
```

### **...**

```
Expand Down
59 changes: 59 additions & 0 deletions solution/0800-0899/0815.Bus Routes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,65 @@ func numBusesToDestination(routes [][]int, source int, target int) int {
}
```

### **C#**

```cs
public class Solution {
public int NumBusesToDestination(int[][] routes, int source, int target) {
if (source == target) {
return 0;
}

Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
List<HashSet<int>> routeToStops = new List<HashSet<int>>();

for (int i = 0; i < routes.Length; i++) {
routeToStops.Add(new HashSet<int>());
foreach (int stop in routes[i]) {
routeToStops[i].Add(stop);
if (!stopToRoutes.ContainsKey(stop)) {
stopToRoutes[stop] = new HashSet<int>();
}
stopToRoutes[stop].Add(i);
}
}

Queue<int> queue = new Queue<int>();
HashSet<int> visited = new HashSet<int>();
int ans = 0;

foreach (int routeId in stopToRoutes[source]) {
queue.Enqueue(routeId);
visited.Add(routeId);
}

while (queue.Count > 0) {
int count = queue.Count;
ans++;

for (int i = 0; i < count; i++) {
int routeId = queue.Dequeue();

foreach (int stop in routeToStops[routeId]) {
if (stop == target) {
return ans;
}

foreach (int nextRoute in stopToRoutes[stop]) {
if (!visited.Contains(nextRoute)) {
visited.Add(nextRoute);
queue.Enqueue(nextRoute);
}
}
}
}
}

return -1;
}
}
```

### **...**

```
Expand Down
54 changes: 54 additions & 0 deletions solution/0800-0899/0815.Bus Routes/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public class Solution {
public int NumBusesToDestination(int[][] routes, int source, int target) {
if (source == target) {
return 0;
}

Dictionary<int, HashSet<int>> stopToRoutes = new Dictionary<int, HashSet<int>>();
List<HashSet<int>> routeToStops = new List<HashSet<int>>();

for (int i = 0; i < routes.Length; i++) {
routeToStops.Add(new HashSet<int>());
foreach (int stop in routes[i]) {
routeToStops[i].Add(stop);
if (!stopToRoutes.ContainsKey(stop)) {
stopToRoutes[stop] = new HashSet<int>();
}
stopToRoutes[stop].Add(i);
}
}

Queue<int> queue = new Queue<int>();
HashSet<int> visited = new HashSet<int>();
int ans = 0;

foreach (int routeId in stopToRoutes[source]) {
queue.Enqueue(routeId);
visited.Add(routeId);
}

while (queue.Count > 0) {
int count = queue.Count;
ans++;

for (int i = 0; i < count; i++) {
int routeId = queue.Dequeue();

foreach (int stop in routeToStops[routeId]) {
if (stop == target) {
return ans;
}

foreach (int nextRoute in stopToRoutes[stop]) {
if (!visited.Contains(nextRoute)) {
visited.Add(nextRoute);
queue.Enqueue(nextRoute);
}
}
}
}
}

return -1;
}
}