
g0001_0100.s0064_minimum_path_sum.Solution.swift Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #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_06_24_Time_17_ms_(92.63%)_Space_16_MB_(21.05%)
class Solution {
func minPathSum(_ grid: [[Int]]) -> Int {
var matrix: [[Int]] = grid
let n = grid.count - 1
let m = grid[0].count - 1
for i in 0...n {
for j in 0...m {
var step = matrix[i][j]
if i > 0 && j > 0 {
step += min(matrix[i - 1][j], matrix[i][j - 1])
} else if i > 0 && j == 0 {
step += matrix[i - 1][j]
} else if j > 0 && i == 0 {
step += matrix[i][j - 1]
}
matrix[i][j] = step
}
}
return matrix[n][m]
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy