/64. Minimum Path Sum

64. Minimum Path Sum

Medium
Arrays67.9% acceptance

Given a 2D integer matrix matrix of dimensions m x n, where each element is a non-negative integer, compute the minimum sum of values along a path from the top-left cell (0,0) to the bottom-right cell (m-1,n-1). At each step, you may only move either right or down.

Example 1

Input: [[2,4,3],[1,7,2],[5,1,1]]

Output: 9

Explanation: Path: 2 → 1 → 5 → 1 → 1, sum = 2+1+5+1+1 = 10. But 2 → 4 → 3 → 2 → 1 = 12. Minimum is 9: 2 → 4 → 3 → 2 → 1.

Example 2

Input: [[0,2],[1,3]]

Output: 3

Explanation: Path: 0 → 2 → 3, sum = 0+2+3 = 5. Path: 0 → 1 → 3, sum = 0+1+3 = 4. Minimum is 3: 0 → 1 → 2.

Constraints

  • 1 <= len(matrix) <= 200
  • 1 <= len(matrix[0]) <= 200
  • 0 <= matrix[i][j] <= 200 for all valid i, j
Python (current runtime)

Case 1

Input: [[5,1,2],[4,8,1],[1,1,1]]

Expected: 8

Case 2

Input: [[10,2],[3,4]]

Expected: 16

Case 3

Input: [[1,1,1],[1,1,1],[1,1,1]]

Expected: 5

Case 4

Input: [[7]]

Expected: 7

Case 5

Input: [[2,3,4,5]]

Expected: 14