Swaps for Two Rising Lists
top and bottom have the same length. One move picks a position i and swaps top[i] with bottom[i].
Return the fewest moves after which both lists are strictly increasing (every item larger than the one before it), or -1 if no moves can do that.
Example 1
top [2,4,9,7]
bottom [1,3,6,10]
returns 1
Swap the last position: the lists become 2, 4, 9, 10 and 1, 3, 6, 7.
Example 2
top [5,3]
bottom [6,3]
returns -1
Position 1 holds 3 and 3: whichever way round, one list has 3 right after its 5.
Constraints
1 <= len(top) == len(bottom) <= 1000 <= top[i], bottom[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Two Trades at MostHard
- Build Array from PermutationBeginner
- Close NeighborsBeginner
- Concatenation of ArrayBeginner
- Deal Into PilesBeginner
- Decompress a Pair ListBeginner
- Find the Highest AltitudeBeginner
- Groups of KBeginner