
Luke L. answered 05/07/22
[DISCOUNTED PRICE] Experienced developer w/ teaching experience
Here is my rough pseudocode to solve this. It requires some dynamic programming (https://en.wikipedia.org/wiki/Dynamic_programming).
To justify O(n^2), we should first imagine if we just had 1 bus line. If we went from one end of the line to the other, we'd be traversing all n stops, which gives us a runtime of O(n). However, since we get one transfer opportunity, we have to take that into account at every step. Let's say we transfer at the first stop. Then we'd essentially be traversing each line twice, or O(2n). But we can also transfer at the second stop. If we did that, it'll be O(3n). And so on and so forth, up to transferring at the nth stop, which, in total, gives us O(n^2).
Of course, this would be much easier to visualize and understand if we could walk through it step-by-step. So if this is unclear or you need more help, I am happy to walk through it with you! Just book a session with me and I will walk through it and explain it until you understand. Right now I am still offering heavily discounted price because I am new to this Wyzant (but not new to tutoring!). Eventually, I will raise it to my regular rate, so book soon!
Sanjay K.
I didn't want it in dynamic programming I wanted it in a graph algorithm ( prim, Kruskal's, dijkstra)05/09/22