Interview Questions/Coding/Minimum Track Sensor Swaps

Minimum Track Sensor Swaps

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Minimum Track Sensor Swaps

Two sensor tracks record one reading at each timestamp. At any timestamp, you may swap the two readings at that same index. Find the minimum number of swaps needed so both tracks become strictly increasing from left to right. The input is guaranteed to have at least one valid answer.

Examples

Example 1
Input:
upper = [1,3,5,4], lower = [1,2,3,7]
Output:
1

Explanation: Swap the readings at the last timestamp.

Approach hint

At each index, only two states matter: swapped or not swapped.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp