Ctrl K

Striped Grid

Medium
You are given a table grid of digits from 0 to 9. In one operation you may replace the number in any cell with any other non-negative number. Make the table striped: - every cell equals the cell below it (if there is one), so each column holds a single number; - every cell differs from the cell to its right (if there is one), so neighbouring columns differ. Return the fewest operations needed.
Example 1
grid=[[3, 3], [3, 5]] → 1

The first column is all 3s already. The second must be uniform and not 3, so repaint its top 3 into 5.

Example 2
grid=[[7], [2], [7]] → 1

A single column: keep the 7s and change the 2.

Example 3
grid=[[4, 4, 4]] → 1

Neighbouring columns must differ, so the middle 4 becomes any other digit.

Constraints
  • 1 ≤ number of rows, number of columns ≤ 1000
  • 0 ≤ grid[i][j] ≤ 9
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWarehouse Cart Routes MediumCheapest Corner-to-Corner Route MediumLargest Solid Square MediumBest Spot for the Turret MediumSteps to the Nearest Zero Medium
loading Python…
Input
grid=[[3, 3], [3, 5]]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 111 tests on the server, hidden ones included.