m × n table grid holds every number from 1 to m · n exactly once. The list order holds the same numbers in some other order.
Walk through order from the start: at step k, paint the cell of the table that contains order[k]. Return the smallest step k (0-based) after which some row or some column of the table is completely painted.order=[4, 1, 6, 2, 5, 3], grid=[[1, 2, 3], [4, 5, 6]] → 1
Step 0 paints 4 and step 1 paints 1. Column 0 holds only 1 and 4, so it is complete after step 1.
order=[2, 3, 1, 4], grid=[[1, 2], [3, 4]] → 2
After steps 0 and 1 the cells 2 and 3 are painted, in different rows and columns. Step 2 paints 1, completing the row [1, 2].
order=[1], grid=[[1]] → 0
grid and order each contain every number from 1 to m · n exactly onceorder=[4, 1, 6, 2, 5, 3], grid=[[1, 2, 3], [4, 5, 6]]
1
Run checks the first 3 tests in your browser. Submit checks all 54 tests on the server, hidden ones included.