mediumDynamic ProgrammingPattern: DP

Optimal Traffic Cycle Synchronization Solution

Problem Statement

Given a travel time matrix and the number of intersections, find the optimal timing cycle for each intersection to minimize the total travel time of all vehicles.

Examples

Example 1:
Input:[[0, 0], [1, 1]]
Output:[0, 0]
Explanation: The optimal timing cycle is [0, 0], where both intersections have cycle 0.
Example 2:
Input:[[10, 20], [30, 40]]
Output:[0, 0]
Explanation: The optimal timing cycle is [0, 0], where both intersections have cycle 0.

Constraints

  • The travel time matrix is a square matrix of size n x n.
  • The input matrix contains non-negative integers.
  • The number of intersections is at least 2.
Time: O(n^3) Space: O(n^2)
Use dynamic programming and memoization to optimize the solution.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.