For the problem in http://oj.leetcode.com/problems/triangle/
I think it can be solved using DFS
let's use T[i][j] to represent the triangle
some pseudo code is like:
int min_sum = MIN_INT;
void findMinPath(int i, int j, int sum){
if(i>n){
min_sum = sum < min_sum ? sum : min_sum;
} else {
sum =+ T[i][j];
}
findMinPath(i+1, j, sum);
findMinPath(i+1, j+1, sum);
}
min_sum is the result
time complexity: O(n^2)
space complexity: O(1) if you don't count stack; O(n) if you count stack
Can you please let me know if you think this doesn't work?
Thank you for any comments