Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created April 21, 2021 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/981b477299ca04591f0c321ae80955e8 to your computer and use it in GitHub Desktop.
Save vrat28/981b477299ca04591f0c321ae80955e8 to your computer and use it in GitHub Desktop.
Min Path Top -Down recursion
class Solution {
func minimumTotal(_ triangle: [[Int]]) -> Int {
let minPath = minTotalHelper(triangle, 0 ,0)
return minPath
}
func minTotalHelper(_ triangle:[[Int]],_ row: Int, _ col:Int) -> Int{
guard row >= 0, col >= 0, row < triangle.count else { return 0}
if col > row { return 0}
let sumSameColumn = minTotalHelper(triangle, row + 1, col)
let sumNextColumn = minTotalHelper(triangle,row + 1, col + 1)
let totalSum = triangle[row][col] + min(sumNextColumn, sumSameColumn)
return totalSum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment