Snowplow to the Road

Easydynamic programmingAlso known as Minimum Path Sum

After a storm, a small farm needs one cleared lane from the barn door to the road. The yard is a grid of squares. The barn door is the top-left square and the gate to the road is the bottom-right one. Drifts are uneven, so each square takes a different number of minutes to plough.

The plough only drives forward, so the lane can only go right or down from square to square. Which lane takes the least time 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 that starts at the top-left square, ends at the bottom-right square, and moves one square right or down at each step.

The total includes both the first and the last square. 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, so that never finishes.

A greedy plough that always picks the cheaper next square is fast but wrong: the second example shows a cheap first step leading 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