File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ int get (vector<vector<int >>& t,int r,int c, vector<vector<int >>& dp){
3
+ if (r==0 && c==0 )
4
+ return dp[r][c]=t[0 ][0 ];
5
+ if (c<0 || c>r)
6
+ return INT_MAX/2 ;
7
+ if (dp[r][c]!=-1 )
8
+ return dp[r][c];
9
+
10
+ vector<int > v=t[r];
11
+ // int ans=INT_MAX/2;
12
+ for (int i=0 ;i<v.size ();i++){
13
+ int ans1=get (t,r-1 ,i,dp);
14
+ int ans2=get (t,r-1 ,i-1 ,dp);
15
+
16
+ // ans=t[r][i]+min(ans,max(ans1,ans2));
17
+ dp[r][i]=t[r][i]+min (ans1,ans2);
18
+ }
19
+ return dp[r][c];
20
+ }
21
+ public:
22
+ int minimumTotal (vector<vector<int >>& traingle) {
23
+ int r=traingle.size ();
24
+ int c=traingle[r-1 ].size ();
25
+ vector<vector<int >> dp (r,vector<int > (c,-1 ));
26
+ int ans=get (traingle,r-1 ,0 ,dp);
27
+ // for(auto i: dp){
28
+ // for(auto j:i){
29
+ // cout<<j<<" ";
30
+ // }
31
+ // cout<<endl;
32
+ // }
33
+ return *min_element (dp[r-1 ].begin (),dp[r-1 ].end ());
34
+ }
35
+ };
You can’t perform that action at this time.
0 commit comments