Snowplow to the Road

Easydynamic programmingAlso known as Minimum Path Sum

After a storm, a farm needs one cleared lane from the barn door (top-left square of the yard grid) to the road gate (bottom-right). Each square takes a different number of minutes to plough.

The plough only drives forward, so the lane can only go right or down. Which lane is quickest to clear?

Your task

Write minClearingTime(snow). snow[r][c] is the minutes needed to clear square (r, c). Return the smallest total time of a lane from the top-left square to the bottom-right square that moves one square right or down at each step.

The total includes the first and last squares. Return only the time, not the lane.

minClearingTime(snow: int[][]) → int

Examples

Example 1
Input
snow = [[2, 1, 4], [3, 8, 1], [6, 2, 1]]
Output
9
Why

Clear 2 → 1 → 4 → 1 → 1 (right, right, down, down): 2 + 1 + 4 + 1 + 1 = 9 minutes. Going down the left side first costs 2 + 3 + 6 + 2 + 1 = 14.

Example 2
Input
snow = [[1, 2, 9], [5, 9, 9], [1, 1, 1]]
Output
9
Why

Always taking the cheaper next square starts 1 → 2, then is stuck between two 9s and ends with at least 14. The best lane pays 5 early to reach the easy bottom row: 1 + 5 + 1 + 1 + 1 = 9.

Constraints

  • 1 ≤ number of rows, number of columns ≤ 100
  • Every row has the same length.
  • 0 ≤ snow[r][c] ≤ 100
Start with brute force

List every right/down lane and add up its minutes. A 100 × 100 yard has about 10⁵⁸ lanes.

Always picking the cheaper next square is fast but wrong: in the second example a cheap first step leads into a costly middle.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code