Skip to content

Commit

Permalink
Add a solution to Bus Routes
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Oct 9, 2022
1 parent 6266047 commit cd6e0de
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions BFS/BusRoutes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Question Link: https://leetcode.com/problems/bus-routes/
* Primary idea: BFS. Build a map for stop and related buses' indexes map. Use a queue to track until the current stop is equal to the target.
*
* Time Complexity: O(nm), Space Complexity: O(nm)
*
*/

class BusRoutes {
func numBusesToDestination(_ routes: [[Int]], _ source: Int, _ target: Int) -> Int {

if source == target {
return 0
}

let stopBusesMap = buildMap(routes)

// bfs
var queue = [source], res = 0, isVisited = Set<Int>()

while !queue.isEmpty {
let size = queue.count

for _ in 0..<size {
let stop = queue.removeFirst()

if stop == target {
return res
}

for busIdx in stopBusesMap[stop] ?? [] {
guard !isVisited.contains(busIdx) else {
continue
}

isVisited.insert(busIdx)
queue.append(contentsOf: routes[busIdx])
}
}

res += 1
}

return -1
}

private func buildMap(_ routes: [[Int]]) -> [Int: Set<Int>] {
var stopBusesMap = [Int: Set<Int>]()

for (i, bus) in routes.enumerated() {
bus.forEach {
stopBusesMap[$0, default: Set<Int>()].insert(i)
}
}

return stopBusesMap
}
}

0 comments on commit cd6e0de

Please sign in to comment.