-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic Programming
272 lines (249 loc) ยท 7.08 KB
/
Dynamic Programming
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
๐๐ฎ๐ฆ๐๐๐ซ ๐จ๐ ๐๐ง๐ข๐ช๐ฎ๐ ๐๐๐ญ๐ก๐ฌ
*Given a A X B matrix with your initial position at the top-left cell, find the number of possible unique paths to reach the bottom-right cell of the matrix from the initial position.
Note: Possible moves can be either down or right at any point in time, i.e., we can move to matrix[i+1][j] or matrix[i][j+1] from matrix[i][j].*
Sol :
int ans(int a,int b){
int dp[a][b];
for(int i=0;i<a;i++){
dp[i][0]=1;
}
for(int i=0;i<b;i++){
dp[0][i]=1;
}
for(int i=1;i<a;i++){
for(int j=1;j<b;j++){
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}}
return dp[a-1][b-1];
}
๐๐จ๐ฎ๐ง๐ญ ๐ฐ๐๐ฒ๐ฌ ๐ญ๐จ ๐ซ๐๐๐๐ก ๐ญ๐ก๐ ๐งโ๐ญ๐ก ๐ฌ๐ญ๐๐ข๐ซ:
int countWaysUtil(int n, int m)
{
int res[n];
res[0] = 1;
res[1] = 1;
for(int i = 2; i < n; i++)
{
res[i] = 0;
for(int j = 1; j <= m && j <= i; j++)
res[i] += res[i - j];
}
return res[n - 1];
}
int countWays(int s, int m)
{
return countWaysUtil(s + 1, m);
}
๐๐๐ฑ๐ข๐ฆ๐ข๐ณ๐ ๐๐ก๐ ๐๐ฎ๐ญ ๐๐๐ ๐ฆ๐๐ง๐ญ๐ฌ:
int maximizeTheCuts(int n, int x, int y, int z)
{
if(n==0) return 0;
int one = (n-x)>0?maximizeTheCuts(n-x,x,y,z):0;
int two = (n-y)>0?maximizeTheCuts(n-y,x,y,z):0;
int three = (n-z)>0?maximizeTheCuts(n-z,x,y,z):0;
return 1+max(one,max(two,three));
}
๐๐ข๐ง๐ข๐ฆ๐ฎ๐ฆ ๐ง๐ฎ๐ฆ๐๐๐ซ ๐จ๐ ๐ฃ๐ฎ๐ฆ๐ฉ๐ฌ:
int minJumps(int arr[], int n){
if(arr[0]==0 || n==0) return -1;
int dp[n];
for(int i=0;i<n;i++){
dp[i]=INT_MAX;
}
dp[0]=0;
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
if(i<=j+arr[j] && dp[i]!=INT_MAX){
dp[i]=min(dp[i],dp[j]+1);
}
}
}
return dp[n-1];
}
๐๐จ๐ญ๐๐ฅ ๐๐๐๐จ๐๐ข๐ง๐ ๐๐๐ฌ๐ฌ๐๐ ๐๐ฌ:
int countDecodingDP(char *digits, int n)
{
int count[n+1];
count[0] = 1;
count[1] = 1;
if(digits[0]=='0')
return 0;
for (int i = 2; i <= n; i++)
{
count[i] = 0;
if (digits[i-1] > '0')
count[i] = count[i-1];
if (digits[i-2] == '1' ||
(digits[i-2] == '2' && digits[i-1] < '7') )
count[i] += count[i-2];
}
return count[n];
}
๐๐จ๐ง๐ ๐๐ฌ๐ญ ๐๐จ๐ฆ๐ฆ๐จ๐ง ๐๐ฎ๐๐ฌ๐๐ช๐ฎ๐๐ง๐๐:
int lcs(int m, int n, string s1, string s2)
{
int L[m + 1][n + 1];
int i, j;
for (i = 0; i <= m; i++)
{
for (j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (s1[i - 1] == s2[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
return L[m][n];
}
๐๐จ๐ง๐ฌ๐๐๐ฎ๐ญ๐ข๐ฏ๐ ๐'๐ฌ ๐ง๐จ๐ญ ๐๐ฅ๐ฅ๐จ๐ฐ๐๐:
ll countStrings(int n) {
ll a[n];
ll b[n];
a[0]=1;
b[0]=1;
for(int i=1;i<n;i++){
a[i]=a[i-1]+b[i-1];
b[i]=b[i-1];
}
return a[n-1]+b[n-1];
}
๐๐๐ข๐ญ ๐๐ข๐ฌ๐ญ๐๐ง๐๐:
int editDistance(string a,string b,int n,int m){
int dp[n+1][m+1];
for(int i=0;i<n+1;i++){
for(int j=0;j<m+1;j++){
if(n==0){
dp[i][j] = j;
}
else if(m==0){
dp[i][j]=i;
}
else if(a[i-1]==b[j-1]){
dp[i][j] = dp[i-1][j-1];
}
else{
dp[i][j] = 1 + + min(dp[i][j - 1],dp[i - 1][j], dp[i - 1][j - 1]);
}
}
}
return dp[n][m];
}
๐๐จ๐ ๐๐ฎ๐ญ๐ญ๐ข๐ง๐ :
int cutRod(int price[], int n)
{
int val[n+1];
val[0] = 0;
int i, j;
for (i = 1; i<=n; i++)
{
int max_val = INT_MIN;
for (j = 0; j < i; j++)
max_val = max(max_val, price[j] + val[i-j-1]);
val[i] = max_val;
}
return val[n];
}
๐๐จ๐ง๐ ๐๐ฌ๐ญ ๐๐ง๐๐ซ๐๐๐ฌ๐ข๐ง๐ ๐๐ฎ๐๐ฌ๐๐ช๐ฎ๐๐ง๐๐:
int lis(int arr[], int n)
{
int lis[n];
lis[0] = 1;
for (int i = 1; i < n; i++) {
lis[i] = 1;
for (int j = 0; j < i; j++)
if (arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
}
return *max_element(lis, lis + n);
}
๐๐๐ฑ๐ข๐ฆ๐ฎ๐ฆ ๐ฌ๐ฎ๐ฆ ๐ข๐ง๐๐ซ๐๐๐ฌ๐ข๐ง๐ ๐ฌ๐ฎ๐๐ฌ๐๐ช๐ฎ๐๐ง๐๐:
int maxSumIS(int arr[], int n)
{
int dp[n];
dp[0]=arr[0];
for(int i=1;i<n;i++){
dp[i]=arr[i];
for(int j=0;j<i;j++){
if(arr[j]<arr[i] && dp[i]<dp[j]+arr[i]){
dp[i]=dp[j]+arr[i];
}
}
}
return *max_element(dp,dp+n);
}
๐๐๐ฑ ๐ฅ๐๐ง๐ ๐ญ๐ก ๐๐ก๐๐ข๐ง:
int maxChainLen(struct val p[],int n)
{
int dp[n];
int i,j=0;
for ( i = 0; i < n; i++ )
dp[i] = 1;
for ( i = 1; i < n; i++ )
for ( j = 0; j < i; j++ )
if ( p[i].first > p[j].second &&
dp[i] < dp[j] + 1)
dp[i] = dp[j] + 1;
int max=0;
for ( i = 0; i < n; i++ )
if ( max < dp[i] )
max = dp[i];
return max;
}
๐๐๐ญ๐๐ซ ๐๐ฏ๐๐ซ๐๐ฅ๐จ๐ฐ:
double waterOverflow(int K, int R, int C) {
double glass[(R*(R-1))/2];
memset(glass,0,sizeof(glass));
glass[0]=K;
for(int i=1;i<R;i++){
for(int j=1;j<i;j++){
int x = glass[i];
if(x<=1.0){
x=0;
}
else{
x = x-1.0;
}
if(x){
x = x/2;
glass[i+1]+=x;
glass[i+2]+=x;
}
return glass[(i*(i-1))/2 + j-1];
}
}
}
๐๐๐ฑ๐ข๐ฆ๐ฎ๐ฆ ๐๐ข๐ฉ ๐๐๐ฅ๐๐ฎ๐ฅ๐๐ญ๐จ๐ซ:
if (n == 0)
return 0;
if (x != 0 and y != 0)
return max(
arr1[n - 1] + maxTip(arr1, arr2, n - 1,x - 1, y),arr2[n - 1] + maxTip(arr1, arr2, n - 1, x,y - 1));
if (y == 0)
return arr1[n - 1] + maxTip(arr1, arr2, n - 1, x - 1, y);
else
return arr2[n - 1] + maxTip(arr1, arr2, n - 1, x, y - 1);
}
๐ - ๐ ๐๐ง๐๐ฉ๐ฌ๐๐๐ค ๐๐ซ๐จ๐๐ฅ๐๐ฆ:
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
vector<vector<int>> K(n + 1, vector<int>(W + 1));
for(i = 0; i <= n; i++)
{
for(w = 0; w <= W; w++)
{
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1] +
K[i - 1][w - wt[i - 1]],
K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}