64. Minimum Path Sum [LeetCode]

/**************************************************************************
 * 
 * 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.
 * 
 **************************************************************************/


/// DFS, Iteration, using ugly C 
int minPathSum(int** grid, int gridSize, int* gridColSize){
    if (NULL == grid || NULL == gridColSize) return 0;
    int *f = (int *)calloc(*gridColSize, sizeof(int));
    f[0] = grid[0][0];
    for (int i = 1; i < *gridColSize; i++)
        f[i] = f[i-1] + grid[0][i];
    for (int i = 1; i < gridSize; i++) {
        for (int j = 0; j < *gridColSize; j++) {
            f[j] = j == 0 ? f[0] + grid[i][0] : fmin(f[j], f[j-1]) + grid[i][j];
        }
    }
    int res = f[*gridColSize-1];
    free(f);
    return res;
}



/// DFS, Iteration, using C++
class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        const int m = grid.size();
        const int n = grid[0].size();
        vector<int> f(n, INT_MAX);
        f[0] = 0;
        for (int i = 0; i < m; i++) {
            f[0] += grid[i][0];
            for (int j = 1; j < n; j++) {
                f[j] = min(f[j - 1], f[j]) + grid[i][j];
            }
        }
        return f[n - 1];
    }
};



/// DFS, Recursion, using ugly C
static int **create(int rows, int cols) {
    if (rows <=0 || cols <= 0) return NULL;  
    int **f = (int **)calloc(rows, sizeof(int *));
    for (int i = 0; i < rows; i++) {
        f[i] = (int *)calloc(cols, sizeof(int));
        for (int j = 0; j < cols; j++) {
            f[i][j] = INT_MAX;
        }
    }
    return f;
}

static void destroy(int **f, int rows) {
    for (int i = 0; i < rows; i++)
        free(f[i]);
    free(f);
}

static int dfs(int **grid, int **f, int x, int y) {
    if (x < 0 || y < 0)  return INT_MAX;
    if (x == 0 && y == 0) return grid[0][0];
    if (f[x][y] ^ INT_MAX) return f[x][y];
    f[x][y] = grid[x][y] + fmin(dfs(grid, f, x-1, y), dfs(grid, f, x, y-1));
    return f[x][y];
}

int minPathSum(int **grid, int gridSize, int *gridColSize){
    if (NULL == grid || NULL == gridColSize || 0 == gridSize || 0 == *gridColSize) 
        return 0;
    //if (1 == gridSize && 1 == *gridColSize) 
    //    return grid[0][0];
    int **f = create(gridSize, *gridColSize);
    f[0][0] = grid[0][0];
    dfs(grid, f, gridSize - 1, *gridColSize - 1);
    int res = f[gridSize - 1][*gridColSize - 1];
    destroy(f, gridSize);
    return res;
}




/// DFS, Recursion, using C++
class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        vector<vector<int> > f(grid.size(), vector<int>(grid[0].size(), INT_MAX));
        return dfs(grid, f, grid.size() - 1, grid[0].size() - 1);
    }
private:
    int dfs(const vector<vector<int> > &grid, vector<vector<int> > &f, int x, int y) {
        if (x < 0 || y < 0)  return INT_MAX;
        if (x == 0 && y == 0) return grid[0][0];
        if (f[x][y] ^ INT_MAX) return f[x][y];
        f[x][y] = grid[x][y] + fmin(dfs(grid, f, x-1, y), dfs(grid, f, x, y-1));
        return f[x][y];
    }
};

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luuyiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值