All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0064_minimum_path_sum.Solution.cs Maven / Gradle / Ivy

namespace LeetCodeNet.G0001_0100.S0064_minimum_path_sum {

// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix
// #Dynamic_Programming_I_Day_16 #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
// #2024_01_05_Time_74_ms_(94.37%)_Space_42.6_MB_(18.50%)

public class Solution {
    public int MinPathSum(int[][] grid) {
        int m = grid.Length;
        int n = grid[0].Length;
        for (int i = 1; i < n; i++) {
            grid[0][i] += grid[0][i - 1];
        }
        for (int i = 1; i < m; i++) {
            grid[i][0] += grid[i - 1][0];
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                grid[i][j] += Math.Min(grid[i][j - 1], grid[i - 1][j]);
            }
        }
        return grid[m - 1][n - 1];
    }
}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy