Max-score grid descent with limited two-row jumps

0
Sign in

You are given an N x M grid of integers board. Rows and columns are numbered from 0, and a cell's value is the score you collect when you visit it. Values may be negative.

A path starts at the top-row cell (0, p) and ends as soon as it reaches any cell in the bottom row (row N - 1). From cell (i, j) the path may move to:

  • (i + 1, j - 1), (i + 1, j) or (i + 1, j + 1): one row down, shifting left, straight, or right.
  • (i + 2, j): a jump straight down two rows. The cell (i + 1, j) in between is skipped and adds nothing.

Every move must land inside the grid. Over the whole path you may use at most K jumps. The score of a path is the sum of the cells it visits, including the start and the last cell. Two paths are different if their lists of visited cells differ.

Implement max_jump_path(board, p, K) that returns three things:

  1. The highest possible path score.
  2. One path with that score, written as (r,c)->(r,c)->.... If several paths tie, return the lexicographically smallest list of cells, comparing (row, col) pairs in order.
  3. The number of distinct paths with that score, modulo 10^9 + 7.

Constraints: 1 <= N, M <= 500, 0 <= p < M, 0 <= K <= N, -10^4 <= board[i][j] <= 10^4.

Example 1

board = [[2,  0,  1],
         [-5, -9, -5],
         [4,  8,  3],
         [1,  2,  6]]
p = 1, K = 1

Output: 14, (0,1)->(2,1)->(3,2), 1. Jumping from (0,1) over the negative row to (2,1) and then stepping down-right gives 0 + 8 + 6 = 14.

Example 2

Same board, K = 0. Output: 9, (0,1)->(1,0)->(2,1)->(3,2), 2. Without a jump, the path must pass through row 1. Going through (1,0) or (1,2) both score 0 - 5 + 8 + 6 = 9, and (1,0) comes first.

OpenAITechnical phone screenMediumLast asked Jul 2026

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.