fork download
  1. /*
  2. PROBLEM:
  3. Reach a Destination Through Scheduled Flights
  4.  
  5. Given a list of flights:
  6. [origin, destination, departureTime, arrivalTime]
  7.  
  8. Determine whether we can reach targetAirport from
  9. startAirport, starting at time 0.
  10.  
  11. A connecting flight is valid if:
  12. nextDepartureTime >= currentArrivalTime.
  13.  
  14. APPROACH: BFS (Flights as Graph Nodes)
  15.  
  16. 1. Treat each flight as a graph node instead of
  17.   treating each airport as a node.
  18.  
  19. 2. Initialize a visited array of size flights.length.
  20.  
  21. 3. Add all flights departing from startAirport
  22.   to the BFS queue.
  23.  
  24. 4. For every flight dequeued:
  25.   - Check if its destination is targetAirport.
  26.   - Iterate through all flights to find valid
  27.   connecting flights.
  28.   - Mark valid unvisited flights as visited
  29.   and enqueue them.
  30.  
  31. 5. If the queue becomes empty without reaching
  32.   targetAirport, return false.
  33.  
  34. WHY VISITED FLIGHTS INSTEAD OF AIRPORTS?
  35.  
  36. Different flights can reach the same airport at
  37. different arrival times.
  38.  
  39. Arriving earlier may allow us to catch connecting
  40. flights that we would otherwise miss.
  41.  
  42. However, each individual flight has a fixed
  43. arrival time. Therefore, processing the same
  44. flight multiple times is unnecessary.
  45.  
  46. TIME COMPLEXITY: O(E^2)
  47.  
  48. E = Number of flights
  49. V = Number of airports
  50.  
  51. - Initialize visited array: O(E)
  52. - Initialize BFS queue: O(E)
  53. - Process at most E flights: O(E)
  54. - Scan all E flights for every processed flight: O(E)
  55.  
  56. Total:
  57. O(E + E + E * E) = O(E^2)
  58.  
  59. SPACE COMPLEXITY: O(E)
  60.  
  61. - Visited array: O(E)
  62. - BFS queue: O(E)
  63. - Temporary variables: O(1)
  64.  
  65. Total: O(E)
  66.  
  67. LIMITATION:
  68. E can be 100,000.
  69.  
  70. O(E^2) = 10,000,000,000 flight checks.
  71.  
  72. This approach may result in TLE.
  73. */
  74.  
  75.  
  76. // ==================== SOLUTION ====================
  77.  
  78. function canReachDestination(
  79. airportCount, flights, startAirport, targetAirport
  80. ) {
  81. if (startAirport === targetAirport) return true;
  82.  
  83. const n = flights.length;
  84. const visited = Array(n).fill(false);
  85.  
  86. const q = [];
  87. let head = 0;
  88.  
  89. // Add all flights departing from startAirport
  90. for (let i = 0; i < n; i++) {
  91. if (flights[i][0] === startAirport) {
  92. q.push(i);
  93. visited[i] = true;
  94. }
  95. }
  96.  
  97. // BFS
  98. while (head < q.length) {
  99. const curr = q[head++];
  100.  
  101. const [origin, destination, departure, arrival] =
  102. flights[curr];
  103.  
  104. // Check if target airport is reached
  105. if (destination === targetAirport) {
  106. return true;
  107. }
  108.  
  109. // Find valid connecting flights
  110. for (let i = 0; i < n; i++) {
  111.  
  112. const [nextOrigin, nextDest, nextDep, nextArr] =
  113. flights[i];
  114.  
  115. if (
  116. !visited[i] &&
  117. nextOrigin === destination &&
  118. nextDep >= arrival
  119. ) {
  120. visited[i] = true;
  121. q.push(i);
  122. }
  123. }
  124. }
  125.  
  126. return false;
  127. }
  128.  
  129.  
  130. // ==================== TEST CASES ====================
  131.  
  132. const tests = [
  133. {
  134. name: "Valid connection at equal time",
  135. airportCount: 3,
  136. flights: [
  137. [0, 1, 2, 5],
  138. [1, 2, 5, 8]
  139. ],
  140. startAirport: 0,
  141. targetAirport: 2,
  142. expected: true
  143. },
  144. {
  145. name: "Missed connecting flight",
  146. airportCount: 3,
  147. flights: [
  148. [0, 1, 2, 6],
  149. [1, 2, 5, 8]
  150. ],
  151. startAirport: 0,
  152. targetAirport: 2,
  153. expected: false
  154. },
  155. {
  156. name: "Multiple routes and unsorted flights",
  157. airportCount: 4,
  158. flights: [
  159. [1, 3, 8, 10],
  160. [0, 2, 1, 4],
  161. [2, 1, 5, 7],
  162. [0, 1, 9, 12]
  163. ],
  164. startAirport: 0,
  165. targetAirport: 3,
  166. expected: true
  167. },
  168. {
  169. name: "Start and target are the same",
  170. airportCount: 3,
  171. flights: [],
  172. startAirport: 0,
  173. targetAirport: 0,
  174. expected: true
  175. },
  176. {
  177. name: "Cycle between airports",
  178. airportCount: 3,
  179. flights: [
  180. [0, 1, 1, 3],
  181. [1, 0, 4, 6],
  182. [0, 2, 7, 9]
  183. ],
  184. startAirport: 0,
  185. targetAirport: 2,
  186. expected: true
  187. },
  188. {
  189. name: "No available flights",
  190. airportCount: 3,
  191. flights: [],
  192. startAirport: 0,
  193. targetAirport: 2,
  194. expected: false
  195. }
  196. ];
  197.  
  198.  
  199. // ==================== TEST RUNNER ====================
  200.  
  201. tests.forEach((test, index) => {
  202.  
  203. const actual = canReachDestination(
  204. test.airportCount,
  205. test.flights,
  206. test.startAirport,
  207. test.targetAirport
  208. );
  209.  
  210. console.log(`Test ${index + 1}: ${test.name}`);
  211. console.log("Expected:", test.expected);
  212. console.log("Actual: ", actual);
  213. console.log(
  214. actual === test.expected ? "PASS" : "FAIL"
  215. );
  216. console.log("------------------------------");
  217. });
Success #stdin #stdout 0.03s 5640KB
stdin
Standard input is empty
stdout
Test 1: Valid connection at equal time
Expected: true
Actual:   true
PASS
------------------------------
Test 2: Missed connecting flight
Expected: false
Actual:   false
PASS
------------------------------
Test 3: Multiple routes and unsorted flights
Expected: true
Actual:   true
PASS
------------------------------
Test 4: Start and target are the same
Expected: true
Actual:   true
PASS
------------------------------
Test 5: Cycle between airports
Expected: true
Actual:   true
PASS
------------------------------
Test 6: No available flights
Expected: false
Actual:   false
PASS
------------------------------