Created
April 21, 2021 15:05
-
-
Save vrat28/981b477299ca04591f0c321ae80955e8 to your computer and use it in GitHub Desktop.
Min Path Top -Down recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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