Problem 534423 · medium · Phase 05 Advanced Algorithms & Graphs

Raindrop on a Window

dynamic programming · grid DP · minimum path

A window pane is a grid pane of integers giving the friction of each glass patch. A raindrop starts on any patch of the top row and slides down one row per step, landing on the patch directly below, or diagonally below-left, or diagonally below-right. It stops when it leaves the bottom row. Return the minimum total friction the drop can experience, counting every patch it touches (including the first).

Examples

Input:  pane = [[2, 9, 3],
                [8, 1, 7],
                [4, 6, 5]]
Output: 7
Explanation: 2 -> 1 -> 4 (start column 0, then column 1, then column 0).

Input:  pane = [[5]]
Output: 5

Constraints

  • 1 <= rows, cols <= 100
  • -100 <= pane[i][j] <= 100
  • Target complexity: O(rows * cols).

Goals

  • Allow three predecessors per cell and clip the ones outside the grid
  • Let the path start anywhere in the first row and end anywhere in the last
Starting Python…