Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    815 - Bus Routes.

    Hard

    You are given an array routes representing bus routes where routes[i] is a bus route that the <code>i<sup>th</sup></code> bus repeats forever.

    • For example, if routes[0] = [1, 5, 7], this means that the <code>0<sup>th</sup></code> bus travels in the sequence 1 -&gt; 5 -&gt; 7 -&gt; 1 -&gt; 5 -&gt; 7 -&gt; 1 -&gt; ... forever.

    You will start at the bus stop source (You are not on any bus initially), and you want to go to the bus stop target. You can travel between bus stops by buses only.

    Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.

    Example 1:

    Input: routes = \[\[1,2,7],3,6,7], source = 1, target = 6

    Output: 2

    Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.

    Example 2:

    Input: routes = \[\[7,12],4,5,15,6,15,19,9,12,13], source = 15, target = 12

    Output: -1

    Constraints:

    • 1 &lt;= routes.length &lt;= 500.

    • <code>1 <= routesi.length <= 10<sup>5</sup></code>

    • All the values of routes[i] are unique.

    • <code>sum(routesi.length) <= 10<sup>5</sup></code>

    • <code>0 <= routesj< 10<sup>6</sup></code>

    • <code>0 <= source, target < 10<sup>6</sup></code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer numBusesToDestination(Array<IntArray> routes, Integer source, Integer target)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait