forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriangle.cpp
More file actions
27 lines (17 loc) · 736 Bytes
/
Triangle.cpp
File metadata and controls
27 lines (17 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int travel(vector<vector<int>>& tr , int lvl , int ind) {
if(ind >= tr[lvl].size()) // To check if we are going out of bound
return INT_MAX;
if(lvl == tr.size() - 1) { // Return if we are on last line
return tr[lvl][ind];
}
int s = travel(tr , lvl + 1, ind ); // Go South
int se = travel(tr , lvl + 1 , ind + 1); // Go South East
// Return the minimum of south and south east + cost of the index we are currently at.
return min(s , se) + tr[lvl][ind];
}
int minimumTotal(vector<vector<int>>& triangle) {
return travel(triangle , 0 , 0);
}
};