-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path120.triangle.cpp
More file actions
192 lines (172 loc) · 5.03 KB
/
120.triangle.cpp
File metadata and controls
192 lines (172 loc) · 5.03 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* @lc app=leetcode id=120 lang=cpp
*
* [120] Triangle
*/
#include "bits/stdc++.h"
using namespace std;
// #include "Tree.h"
#define deb(x) cout<<x<<endl;
const int inf = 1e9;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int,int> pii;
// #include "LinkedList.h"
void print(vi &out){
for(auto x: out) cout<<x<<" ";
cout<<endl;
}
// @lc code=start
// class Solution {
// public:
// int minimumTotal(vector<vector<int>>& triangle){
// int n= triangle.size();
// vector<vector<int>> dp(n,vector<int>(n,-1));
// if(n==1)
// return triangle[0][0];
// return help(0,0,triangle,dp);
// }
// int help(int row, int col, vector<vector<int>>& tri,vector<vector<int>>& dp){
// if(dp[row][col]!=-1)
// return dp[row][col];
// if(row == tri.size()-2){ // row=1
// dp[row][col] = tri[row][col]+min(tri[row+1][col],tri[row+1][col+1]);
// return dp[row][col];
// }
// dp[row][col] = tri[row][col]+ min(help(row+1,col,tri,dp), help(row+1,col+1,tri,dp));
// return dp[row][col];
// }
// };
// class Solution {
// public:
// int minimumTotal(vector<vector<int>>& triangle){
// int n= triangle.size();
// if(n==1) return triangle[0][0];
// vector<int> row = triangle.back();
// for(int i=n-2;i>=0;i--){
// for(int j=0;j<triangle[i].size();j++){
// row[j] = triangle[i][j] + min(row[j],row[j+1]);
// }
// }
// return row[0];
// }
// };
// class Solution {
// public:
// int minimumTotal(vector<vector<int>>& triangle){
// int n= triangle.size();
// for(int i=n-2;i>=0;i--){
// for(int j=0;j<triangle[i].size();j++){
// triangle[i][j] += min(triangle[i+1][j],triangle[i+1][j+1]);
// }
// }
// return triangle[0][0];
// }
// };
// recursion
class Solution0 {
public:
int f(int i, int j, vector<vector<int>>& triangle){
int m = triangle.size();
int n = triangle[0].size();
if(i==m-1) return triangle[m-1][j];
int down= triangle[i][j]+ f(i+1,j, triangle);
int diag= triangle[i][j]+ f(i+1,j+1, triangle);
return min(down,diag);
}
int minimumTotal(vector<vector<int>>& triangle){
return f(0,0, triangle);
}
};
// memoization
class Solution1 {
public:
int f(int i, int j, vector<vector<int>>& triangle, vector<vector<int>>& dp){
int m = triangle.size();
if(i==m-1) return triangle[m-1][j];
if(dp[i][j]!=-1) return dp[i][j];
int down= triangle[i][j]+ f(i+1,j, triangle,dp);
int diag= triangle[i][j]+ f(i+1,j+1, triangle,dp);
return dp[i][j]=min(down,diag);
}
int minimumTotal(vector<vector<int>>& triangle){
int m = triangle.size();
vector<vector<int>> dp(m, vector<int>(m,-1));
return f(0,0, triangle,dp);
}
};
// tabulation
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle){
int m = triangle.size();
vector<vector<int>> dp(m, vector<int>(m,0));
for(int j=0;j<m;j++){
dp[m-1][j]= triangle[m-1][j];
}
for(int i=m-2;i>=0;i--){ // m-2 cause already calculated for m-1
for(int j=i;j>=0;j--){
int down = triangle[i][j]+ dp[i+1][j];
int diag= triangle[i][j]+ dp[i+1][j+1];
dp[i][j] = min(down, diag);
}
}
return dp[0][0];
}
};
// space optimization
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle){
int m = triangle.size();
vector<int> prev(n,0);
for(int j=0;j<m;j++){
prev[j]= triangle[m-1][j];
}
for(int i=m-2;i>=0;i--){
vector<int> curr(n,0);
for(int j=i;j>=0;j--){
int down = triangle[i][j]+ prev[j];
int diag= triangle[i][j]+ prev [j+1];
curr[j] = min(down, diag);
}
prev= curr;
}
return prev[0];
}
};
// @lc code=end
class Solution {
public:
int minimumTotal(vector<vector<int>>& tri) {
int n= tri.size();
int m= tri[0].size();
vector<vector<int>> dp(n, vector<int>(n));
dp[0][0] = tri[0][0];
for(int i=1; i<n; i++){
for(int j=0; j<=i; j++){
int diag = INT_MAX;
if(j>0)
diag = tri[i][j] + dp[i-1][j-1];
int up =INT_MAX;
if(j<i)
up = tri[i][j] + dp[i-1][j];
dp[i][j] = min(up, diag);
}
}
int ans =INT_MAX;
for(int j=0; j<n; j++){
ans = min(ans, dp[n-1][j]);
}
return ans;
}
};
int main(){
Solution sol;
vector<vector<int>> tri={{2},{3,4},{6,5,7},{4,1,8,3}};
int ans = sol.minimumTotal(tri);
cout<<ans;
return 0;
}