mediumBacktrackingPattern: Backtracking

Distinct Traffic Light Mode Sequences Solution

Problem Statement

Given integers n and k, find the total number of valid sequences of traffic light modes such that no two adjacent intersections have the same mode.

Examples

Example 1:
Input:{"n":3,"k":2}
Output:6
Explanation: There are six valid sequences: 0, 0, 1; 0, 1, 0; 0, 1, 1; 1, 0, 0; 1, 0, 1; 1, 1, 0

Constraints

  • 1 <= n <= 14
  • 1 <= k <= 100
  • n <= 14
Time: O(2^n * n) Space: O(2^n * n)
Apply dynamic programming and memoization to improve the efficiency of the solution by storing the count of valid sequences for each subproblem and avoiding redundant calculations.

Run, Test & Submit Code

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

Solve on Interactive Workspace

Tested Solutions

// code goes here print(distinct_traffic_light_mode_sequences(10, 3));