Bouncing Square Reaches a Screen Corner
Problem statement
A 2 x 2 square moves diagonally across a rectangular screen. Its position is represented by the row and column of its top-left cell.
On every step, the square moves one row and one column in its current direction. If that move would cross a horizontal or vertical screen boundary, the corresponding direction component reverses before the square moves.
The input direction is one of "up-left", "up-right", "down-left", or "down-right".
Return the minimum number of steps until the square covers any corner of the screen. The starting position counts, so return 0 when the square already covers a corner. If the square will never cover a corner, return -1.
Function
stepsToScreenCorner(size: int[], position: int[], direction: String) → intExamples
Example 1
size = [6,18]position = [3,1]direction = "up-right"return = 15The square repeatedly reflects while its top-left cell stays within rows 0 through 4 and columns 0 through 16. After 15 moves, it first reaches a top-left position that makes the square cover a screen corner.
Example 2
size = [6,6]position = [1,1]direction = "up-right"return = -1The position-and-direction state eventually repeats without covering any screen corner, so no future step can succeed.
Constraints
size.length = 2and both dimensions are at least2.position.length = 2.0 <= position[0] <= size[0] - 2.0 <= position[1] <= size[1] - 2.directionis one of the four supported diagonal direction strings.