The Grace Hopper framing threw me for a second, felt like a trick.
Model the problem as a dynamic programming over intervals, where the state tracks the process assigned to the previous interval. Use a recurrence that sums over all processes except the previous one, and optimize with prefix sums to achieve O(mn) time. Handle the base case for the first interval and return the sum of valid assignments for the last interval modulo 10^9+7.
Pro tip: Clarify edge cases upfront (e.g., n=1 and m>1 yields 0 ways) and mention that the DP can be optimized to O(m) space by keeping only the previous row. This shows attention to both correctness and efficiency.
Let dp[i][j] be the number of valid assignments for the first i intervals where the i-th interval is assigned process j. Initialize dp[1][j] = 1 for all j.
For i > 1, dp[i][j] = sum_{k != j} dp[i-1][k]. This ensures no process appears in two consecutive intervals.
Compute total = sum_k dp[i-1][k] and prefix sums to get dp[i][j] = total - dp[i-1][j] in O(1) per state, reducing time to O(mn).
Apply modulo 10^9+7 at each addition/subtraction. If n=1 and m>1, return 0; if m=1, return n.
After filling the DP table, the answer is sum_j dp[m][j] modulo 10^9+7. Optionally, reduce space to O(n) by keeping only the previous row.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.