Minimum Moves to Spread Stones Across a Grid
Updated
Problem statement
You are given a 3 x 3 integer grid. Each value is the number of stones in that cell. The grid holds exactly 9 stones in total.
In one move, you may take one stone from a cell and put it in a neighboring cell directly above, below, left, or right of it. Return the minimum number of moves needed so that every cell holds exactly one stone.
Function
minimumMovesToSpreadStones(grid: int[][]) → intExamples
Example 1
grid = [[1,1,0],[1,1,2],[1,1,1]]return = 1The middle-right cell holds an extra stone, and the top-right cell just above it is empty. Moving that stone up fixes both cells, so a single move is enough.
Example 2
grid = [[1,3,0],[1,0,0],[1,0,3]]return = 4The top-middle cell keeps a single stone and passes its extras to the empty cells on its right and below it. The bottom-right cell does the same with the empty cells above it and to its left. Each extra stone moves just a single step, and no stone can do better, so the answer is 4.
Example 3
grid = [[1,1,1],[1,1,1],[1,1,1]]return = 0Every cell already holds exactly one stone, so no moves are needed.
Constraints
grid.length = 3.grid[i].length = 3.0 <= grid[i][j] <= 9.- The sum of all values in
gridis exactly9.
More American Express interview questions
- Maximum Points in a Perimeter-Bounded RectangleOA · Seen Sep 2026
- Largest Two Non-Overlapping Buildable SquaresOA · Seen Aug 2026
- Maximum Sum of Two Sign-Flipping WindowsOA · Seen Aug 2026
- Crop Text at a Whole-Word BoundaryOA · Seen Aug 2026
- Minimum Parking Roof LengthOA · Seen Aug 2026
- Minimum Pile Reduction StepsOA · Seen Aug 2026
- Disjoint Target-Sum TripletsPHONE SCREEN · Seen Mar 2025
Similar questions at other companies
- Count Eight-Neighbor Unlock PatternsGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum Words in Disjoint Grid PathsIndeed · PHONE SCREENIndeed interview questions
- Count Routes Through Four Shop TypesOpenAI · OAOpenAI interview questions
- Grid Paths with Override PassesIMC Trading · OAIMC Trading interview questions
- Grid Paths with Override PassesMicrosoft · OAMicrosoft interview questions
- Vehicle Builds for Required Terrain (Game Adaptation)Roblox · OARoblox interview questions
- Maximum Profit Under a Bitwise OR LimitConcentric AI · OAConcentric AI interview questions
- Bitwise XOR SubsequencesJP Morgan Chase · OAJP Morgan Chase interview questions
- Beautiful Number OrderingsDocuSign · OADocuSign interview questions
- Paint a Grid with Three ColorsIntuit · OAIntuit interview questions