-
Notifications
You must be signed in to change notification settings - Fork 14
/
notes.txt
364 lines (261 loc) · 6.88 KB
/
notes.txt
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//memo
if(i == n) ret 0
check vis
if dp[i][pi+1] != -1 ret dp[i][pi+1]
//not take
int len = 0 + f(arr,n,i+1,pi,dp)
if(pi == -1 || arr[i]>arr[pi])
//take
len = max(len, 1+ f(arr,n,i+1,i,dp))
return dp[i][pi+1] = len;
//tab
//do not need base case
int[][] dp = new int[n+1][n+1]
for(int i = n-1, i >= 0, i--)
for(int pi = i-1, pi >= -1, pi--)
int len = 0 + dp[i+1][pi+1]
if(pi == -1 || arr[i]>arr[pi])
//take
len = max(len, 1+ dp[i+1][i+1])
return dp[i][pi+1] = len;
//space opt
//at all the i make it current, at all i+1 make it next
int[] curr = new int[n+1]
int[] next = new int[n+1]
for(int i = n-1, i >= 0, i--)
for(int pi = i-1, pi >= -1, pi--)
int len = 0 + next[pi+1]
if(pi == -1 || arr[i]>arr[pi])
//take
len = max(len, 1+ next[i+1])
return curr[pi+1] = len;
-------------------------LCS------------------------------------------------------
LCS - RECURSION
f(i1,i2)
//base
if(i1<0 || i2<0)
return 0
//match
if(sa[i1] == sb[i2])
return 1 + f(i1-1,i2-1)
//not match
else
return 0+ max(f(i1-1,i2), f(i1,i2-1))
//memo
f(s1,s2,i1,i2.int[][]dp)
if(i1<0 or i2<0)
return 0
//visited subproblem
if(dp[i1][i2] != -1)
return dp[i1][i2]
//match
if(s1.charAt(i1) == s2.charAt(i2))
return dp[i1][i2] = 1 + f(s1,s2,i1-1,i2-1,dp)
else
//not match
return dp[i1][i2] = 0 + max(f(s1,s2,i1-1,i2,dp),f(s1,s2,i1,i2-1,dp));
lcs(s1,s2)
int n = len(s1)
int m = len(s2)
int[][] dp = new int[n][m]
for(int rows[]: dp)
Arrays.fill(rows,-1);
return f(s1,s2,n-1,m-1,dp);
//tabulation - eliminate the auxx stack space by using nested for loops and discarding the
recursion tree
//tab -> bottom up
//base case will be altered
// recurrence will be same but instead in for loops
a|e
0 0
i1-1 -> 0-1 -> -1-> recursion is over
recursive code -> -1, 0, 1, 2,.....,n
tab will co-ord shift -> 0. 1, 2, 3......,n+1
so now to alter base case-> simply do i1==0 or i2==0 then return 0
ie. dp[i1][0] = 0
ie. dp[0][i2] = 0
//tab code
lcs(s1,s2)
int n = len(s1)
int m = len(s2)
int[][] dp = new int[n+1][m+1]
for(int rows[]: dp)
Arrays.fill(rows,-1);
//base case as discussed
for(int i=0, i<=n;i++)
dp[i][0] = 0
for(int i=0; i<=m; i++)
dp[0][i] = 0
//main logic
//since 0 is covered in base case start from 1
for(int i1=1; i1<=n; i1++)
for(int i2=1; i2<=m; i++)
//match
if(s1.charAt(i1-1) == s2.charAt(i2-1))
dp[i1][i2] = 1 + dp[i1-1][i2-1];
//not match
else
dp[i1][i2] = 0 + max(dp[i1-1][i2],dp[i1][i2-1]);
return dp[n][m]
//space opt
dp[i1-1] to prev
dp[i1] to curr
lcs(s1,s2)
int n = len(s1)
int m = len(s2)
int prev[] = new int[m+1]
int curr[] = new int[m+1]
//main logic
//since 0 is covered in base case start from 1
for(int i1=1; i1<=n; i1++)
for(int i2=1; i2<=m; i++)
//match
if(s1.charAt(i1-1) == s2.charAt(i2-1))
curr[i2] = 1 + prev[i2-1];
//not match
else
curr[i2] = 0 + max(prev[i2],curr[i2-1]);
//since we need to update prev to curr for next iterations
prev = (int[])(curr.clone());
return prev[m] or curr[m]
----------------------------------------EDIT DISTANCE-----------------------------------------
recr
def md(self,w1,w2):
if not w1 and not w2:
return 0
if not w1:
return len(w2)
if not w2:
return len(w1)
if w1[0] == w2[0]:
return self.md(w1[1:],w2[1:])
insert = 1 + self.md(w1, w2[1:])
delete = 1 + self.md(w1[1:],w2)
replace = 1 + self.md(w1[1:]+w2[1:])
return min(insert,delete,replace)
//convert thus recr into memo
def md(self,w1,w2,i,j,memo):
if i == len(w1) and j == len(w2):
return 0
if i == len(w1):
return len(w2)-j
if j == len(w2):
return len(w1) - i
if i,j not in memo:
if w1[i] == w2[j]:
ans = self.md(w1,w2,i+1,j+1,memo)
else:
insert = 1 + self.md(w1,w2,ij+1,memo)
delete = 1 + self.md(w1,w2,i+1,j,memo)
replace = 1 + self.md(w1,w2,i+1,j+1,memo)
ans = min(insert,delete,replace)
memo[(i,j)] = ans
return memo[(i,j)]
//tabulation
def md(self,w1,w2):
m = len(w1)
n = len(w2)
tbl = [[0]*(n+1) for _ in range(m+1)]
for i in range(m+1):
tbl[i][0] = i
for j in range(n+1):
tbl[0][j] = j
for i in range(1,m+1):
for j in range(1,n+1):
if w1[i-1] == w2[j-1]:
tbl[i][j] = tbl[i-1][j-1]
else:
tbl[i][j] = 1 + min(tbl[i][j-1], tbl[i-1][j], tbl[i-1][j-1])
return tbl[-1][-1]
------------------------------------------------------MAX SUM INCR SUBSEQ [MSIS]-----------------------------------------------------------
//tab
//bottm up
int f(int[] arr, int n)
//init
int i;
int j;
int max = 0;
//init table
int[] msis = new int[n];
//fill msis with arr[i]
for(int i = 0; i < n; i++)
msis[i] = arr[i];
//calc max sum values in bottom up fashion
for(int i = 1; i < n ; i++)
for(int j = 0; j< i; j++)
if(arr[i] > arr[j] && msis[i] < msis[j] + arr[i])
msis[i] = msis[j] + arr[i]
//pick max of all msis vals
for(int i = 0; i < n; i++)
if(msis[i] > max)
max = msis[i];
return max;
---------------------------------------------MINIMUM PATH SUM-----------------------------------------------
min-path sum
//recr
f(i,j)
//base case
if(i==0 && j==0) return matx[0][0]
//idx out of bnd
if(i < 0 || j < 0) return INT_MAX(1e9)
//main logic - directions -> UP , LEFT
up = matx[i][j] + f(i-1,j)
left = matx[i][j] + f(i, j-1)
return min(up,left)
//memo
//create a dp array of size nm
//check if visited subproblem
//if visited return
//if not visited find answer and update value in dp
f(i, j, int[][] matx, int[][]dp)
//base case
if(i == 0 && j==0) return matx[0][0]
//edge case - out of boundary
if(i<0 || j<0)
return (int) Math.pow(10,9);
//check if problem is visited the return val
if(dp[i][j] != -1) return dp[i][j]
//not visited -> go up and left
int up = matx[i][j] + f(i-1,j,matx,dp);
int left = matx[i][j] + f(i,j-1,matx,dp);
return dp[i][j] = Math.min(up,left);
minpth(int n, int m, int[][]mtx)
int[][] dp = new int[n][m];
for(int row[]: dp)
Arrays.fill(row,-1)
return f(n-1,m-1,mtx,dp);
tc - o(mn)
sc - o(m-1)+o(n-1)+ o(nm)DP
//convert memo to tabln for eliminating ass
minpth(n,m,int[][]mtx)
int dp[][] = new int[n][m]
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(i==0 && j==0) -> dp[i][j] = mtx[i][j]
else
int up = mtx[i][j]
if(i>0) up += dp[i-1][j]
else up+= (int)Math.pow(10,9)
int left = mtx[i][j];
if(j>0) left+= dp[i][j-1]
else left += (int) Math.pow(10,9)
dp[i][j] = Math.min(up,left)
return dp[n-1][m-1]
//space opt
//place prev ipo dp[i-1] and curr ipo dp[i]
minpth(int n, int m, int[][] mtx)
int prev[] = new int[n]
for(int i=0; i<n; i++)
int curr[] = new int[m];
for(int j=0; j < m; j++)
if(i==0 && j==0) curr[j] = mtx[i][j];
else
int up = mtx[i][j]
if(i>0) up += prev[j]
else up += (int) Math.pow(10,9)
int left = mtx[i][j]
if(j>0) left += curr[j-1];
else left += (int) Math.pow(10,9);
curr[j] = Math.min(up,left);
rev = curr;
return prev[m-1]