64.Minimum-Path-Sum
64. Minimum Path Sum
题目地址
https://leetcode.com/problems/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 right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.代码
Approach 1: DFS - bottom up
Complexity Analysis
Time complexity : O(2^(_m+_n)). For every move, we have atmost 2 options.
Space complexity : O(m+n). Recursion of depth m+n.
Approach #2 DFS - top up
Approach #3 DP
Complexity Analysis
Time complexity : O(mn). We traverse the entire matrix once.
Space complexity : O(mn). Another array of row size is used.
Approach #4 Dynamic Programming 2D
Approach #5 Dynamic Programming 1D
Complexity Analysis
Time complexity : O(mn). We traverse the entire matrix once.
Space complexity : O(n). Another array of row size is used.
Approach #6 Dynamic Programming (Without Extra Space)
Complexity Analysis
Time complexity : O(mn). We traverse the entire matrix once.
Space complexity : O(1). No extra space is used.
Last updated
Was this helpful?