知之者 不如好之者, 好之者 不如樂之者

기계처럼 살지 말고, 즐기는 인간이 되자

Code/LeetCode

[LeetCode] 64. Minimum Path Sum (Medium/Python)

코방코 2023. 3. 27. 21:20
728x90
 

Minimum Path Sum - LeetCode

Can you solve this real interview question? Minimum Path Sum - Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. Note: You can only move either down or rig

leetcode.com

Above is the link to the problem.

 

Problem

Given a m x n grid filled with non-negative numbers,

find a path from the top left to the bottom right,

which minimizes the sum of all numbers along its path.

You can only move either down or right at any point in time.

 

Example

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

Output: 7

Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.

 

Analysis

Let's think about the below 4x4 grid example.

We can solve this problem by finding the minimum summation path for each grid. 

The grid of the column[0] and row[0] summation counts easily because there is only one path.

about column[0]

about row[0]

Then, we can choose the minimum path for each grid from the top-left grid to the bottom-right grid.

 

 

Code

 

 

This code gets the top 39.27% of time complexity and uses 15.7MB of memory

 

 

728x90
반응형