class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int n = triangle.size();
if( n == 1) {
return triangle[0][0];
}
for( int i = n-2 ; i >= 0 ; i-- ) {
for( int j = 0; j < triangle[i].size() ; j++ ) {
triangle[i][j] = min(triangle[i][j] + triangle[i+1][j], triangle[i][j] + triangle[i+1][j+1]);
}
}
return triangle[0][0];
}
};
Arpit's Newsletter read by 15000+ engineers
🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.