FunctionRacerProblemsRace

Ramp Drop

MediumGridsPython, JavaScript, TypeScript, Java, C++

Every cell of grid holds a ramp: 1 is a ramp that slopes down towards the right and -1 is a ramp that slopes down towards the left. One ball is dropped into the top of each column. A ball that is in column c of some row slides to column c + 1 of the row below when the cell is 1 and the cell to its right in the same row is also 1; it slides to column c - 1 of the row below when the cell is -1 and the cell to its left is also -1. In every other case the ball gets stuck: the ramp points at the side of the grid, or at a cell whose ramp points back, which makes a V. Return a list with one entry per starting column, from left to right: the column where that ball comes out of the bottom, or -1 if it gets stuck.

Example 1

grid [[1,1,-1],[-1,-1,1],[1,1,1]]
returns [1,-1,-1]

Ball 0 slides right to column 1, back left to column 0 in the second row, then right again in the last row and comes out of column 1. Balls 1 and 2 meet in the V between columns 1 and 2 of the top row.

Example 2

grid [[1,1,-1,-1]]
returns [1,-1,-1,2]

In the only row, ball 0 slides to column 1 and comes out there; ball 3 slides left to column 2; balls 1 and 2 meet in a V.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems