My swift simple solution


  • 0
    F
    class Solution {
      func minPathSum(_ grid: [[Int]]) -> Int {
        
        var dp = [Int](repeatElement(Int.max, count: grid.first?.count ?? 0))
        dp[0] = 0
        
        for row in grid {
          
          for j in 0..<row.count {
            
            if j == 0 {
              dp[j] += row[j]
            } else {
              dp[j] = min(dp[j], dp[j - 1]) + row[j]
            }
          }
        }
        
        return dp.last ?? 0
      }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.