MediumArrayDynamic ProgrammingMatrix

Minimum Path Sum

LeetCode
1 approach, code in all languages

You are given an m x n grid filled with non-negative numbers. Starting from the top-left cell you want to reach the bottom-right cell, and on each move you may travel only one cell to the right or one cell down.

Return the smallest possible sum of all numbers along a valid path from the top-left corner to the bottom-right corner.

Example 1

Input: grid = [[1,3,1],[1,5,1],[4,2,1]]

Output: 7

The route 1 -> 3 -> 1 -> 1 -> 1 accumulates the minimum total of 7.

Example 2

Input: grid = [[1,2,3],[4,5,6]]

Output: 12

The path 1 -> 2 -> 3 -> 6 sums to 12, the least achievable for this grid.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 200
  • 0 <= grid[i][j] <= 200
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now