Repository navigation
Expand file tree
/
Copy pathDP.cpp
More file actions
1611 lines (1328 loc) · 38.3 KB
/
Copy pathDP.cpp
File metadata and controls
1611 lines (1328 loc) · 38.3 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <cmath>
#include <limits.h>
#include <vector>
#include <string>
#include <typeinfo>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
using namespace std;
// Fibonacci Number Using Top-Down M1
int FibMemo(int n, vector<int> &dp){
// Base Case
if(n==0 || n==1){
return n;
}
// Memoization
if(dp[n] != -1){
return dp[n];
}
// Recursion Call and Processing
dp[n]= FibMemo(n-1, dp) + FibMemo(n-2, dp);
return dp[n];
}
int FibonacciNumberM1(int n){
vector<int> dp(n+1, -1);
return FibMemo(n, dp);
}
// Fibonacci Number Using Bottom-Up M2
int FibTab(int n){
// Edge case
if(n==0){
return 0;
}
// Tabulation
vector<int> dp(n+1, -1);
// Known Values Storing
dp[0]= 0;
dp[1]= 1;
// Recursion to Iteration
for(int i=2; i<n+1; i++){
dp[i]= dp[i-1] + dp[i-2];
}
return dp[n];
}
// Fibonacci Number Using Bottom-Up Space Optimization M3
int FibTabSO(int n){
if(n==0){
return 0;
}
// Known Values, SO
int prev= 0;
int curr= 1;
// Tabulation
for(int i=2; i<n+1; i++){
int temp= prev + curr;
prev=curr;
curr=temp;
}
return curr;
}
// House Robber 1
// Memoization House Robber Problem M1
int RobberMemo(vector<int> &nums, int index, vector<int> &dp){
// Base Case
if(index>= nums.size()){
return 0;
}
if(dp[index] != -1){
return dp[index];
}
// Recursion Call and Processing
int opt1= nums[index] + RobberMemo(nums, index+2, dp);
int opt2= RobberMemo(nums, index+1, dp);
// Memoization, Max from every index point
dp[index]= max(opt1, opt2);
return dp[index];
}
int HouseRobber1(vector<int> &nums){
int index= 0;
int n= nums.size();
vector<int> dp(n, -1);
return RobberMemo(nums, index, dp);
}
// Tabulation House Robber Problem M2
int HouseRobber1Tab(vector<int> &nums){
int n= nums.size();
vector<int> dp(n, -1);
// Known Value
// n-2 store isliye nai kiya kyuki [2,1,1,2] me 1,2 me se hamesha 2 me hi chori hogi, toh index-2 wala 1 kabhi chori hi nai hoga
dp[n-1]= nums[n-1];
// Tabulation
for(int index=n-2; index>=0; index--){
int tempAns= 0;
// Out of bound case
if(index +2 < n){
tempAns= dp[index+2];
}
int opt1= nums[index] + tempAns;
int opt2= dp[index+1];
dp[index]= max(opt1, opt2);
}
// Return dp[0] isliye kyuki dp0 se most profitable way hai woh, 0 index rakhna ya nai woh uski choice ho gyi
return dp[0];
}
// Tabulation Space Optimization House Robber Problem M3
int HouseRobber1TabSO(vector<int> &nums){
int n= nums.size();
// Edge Case
if(n==1){
return nums[0];
}
// Known Values, SO, Bound ke bahar isliye 0
int next= 0;
int prev= nums[n-1];
int curr;
// Tabulization
for(int index=n-2; index>=0; index--){
// Order is curr prev next
int opt1= nums[index] + next;
int opt2= prev;
// curr
curr= max(opt1, opt2);
// Iterators
next=prev;
prev= curr;
}
return prev;
}
// House Robber 2, Circular houses, Same Memoization
int HouseRobber2(vector<int> &nums){
// Single Case, cant pop_back
if(nums.size() == 1){
return nums[0];
}
// Popping last
int temp= nums.back();
nums.pop_back();
// Max from 0 to n-1
int index= 0;
int n= nums.size();
vector<int> dp1(n, -1);
int inc1= RobberMemo(nums, index, dp1);
// Pushing back temp
nums.push_back(temp);
// Max from 1 to n
index= 1;
n= nums.size();
vector<int> dp2(n, -1);
int exc1= RobberMemo(nums, index, dp2);
// Returning the max
return max(inc1, exc1);
}
// Coin Change
// Memoization Coin Change Problem M1
int CoinChangeMemo(vector<int> &coins, int amount, vector<int> &dp){
// Base Case
if(amount == 0){
return 0;
}
if(dp[amount] != -1){
return dp[amount];
}
int mini= INT_MAX;
// Memoization
// Recursion Call & Processing
for(int i=0; i<coins.size(); i++){
// Only for +ve values
if(amount - coins[i] >= 0){
int recAns= CoinChangeMemo(coins, amount-coins[i], dp);
// coins[i] == [2] && amount == 3 jese cases
if(recAns != INT_MAX){
int ans= 1 + recAns;
mini= min(mini, ans);
}
}
}
dp[amount]= mini;
return dp[amount];
}
int CoinChangeM1(vector<int> &coins, int amount){
// Uss amount pe konsa best way hai
// amount+1 indexing cause amount access krni hai
vector<int> dp(amount+1, -1);
int ans= CoinChangeMemo(coins, amount, dp);
if(ans == INT_MAX){
return -1;
}
return ans;
}
// Tabulation Coin Change Problem M2
int CoinChangeTab(vector<int> &coins, int amount){
vector<int> dp(amount+1, INT_MAX);
// Known Value, 0 hone pe toh kuch nai chahiye
dp[0]=0;
// Tabulation
for(int value=1; value < amount+1; value++){
int mini= INT_MAX;
for(int i=0; i<coins.size(); i++){
// Only for +ve values
if(value - coins[i] >= 0){
int valueAns= dp[value- coins[i]];
// coins[i] == [2] && amount == 3 jese cases
if(valueAns != INT_MAX){
int ans= 1 + valueAns;
mini= min(mini, ans);
}
}
}
dp[value]= mini;
}
return dp[amount];
}
int CoinChangeM2(vector<int> &coins, int amount){
int ans=CoinChangeTab(coins, amount);
if(ans == INT_MAX){
return -1;
}
return ans;
}
// Painting the Fence
// Memoization Painting the Fence Problem M1
int PaintFenceMemo(int n, int k, vector<int> &dp){
// Base case
if(n==1){
return k;
}
if(n==2){
return k+ k*(k-1);
}
if(dp[n] != -1){
return dp[n];
}
dp[n]= (k-1)*(PaintFenceMemo(n-2, k, dp) + PaintFenceMemo(n-1, k, dp));
return dp[n];
}
int PaintFence(int n, int k){
vector<int> dp(n+1, -1);
int ans= PaintFenceMemo(n, k, dp);
return ans;
}
// Tabulation Painting the Fence Problem M2
int PaintFenceTab(int n, int k){
// Edge Case
if(n==1){
return k;
}
// Known Values
vector<int> dp(n+1, -1);
dp[1]= k;
dp[2]= k+ k*(k-1);
// Tabulation
for(int i=3; i<n+1; i++){
dp[i]= (k-1)*(dp[i-2] + dp[i-1]);
}
return dp[n];
}
// Tabulation Space Optimization Painting the Fence Problem M3
int PaintFenceTabSO(int n, int k){
// Edge Case
if(n==1){
return k;
}
// Known Values, SO
int prev= k;
int next= k+ k*(k-1);
int curr;
// Tabulation
for(int i=3; i<n+1; i++){
// Order is prev next curr
curr= (k-1)*(prev + next);
prev= next;
next= curr;
}
// n=2 ke case me atak jaega
return next;
}
// Knapsack
// Memoization KnapSack Problem M1
int KnapsackMemo(int capacity, vector<int> &val, vector<int> &wt, vector<vector<int>> &dp, int index){
// Base Case
// If index== size-1, krenge toh always include wala case ho jaega kyuki return wahi hoga
if(index >= val.size()){
return 0;
}
if(dp[capacity][index] != -1){
return dp[capacity][index];
}
// Memoization
// Include
// Ho sakta hai include nikle hi nai
int include = INT_MIN;
if(wt[index] <= capacity){
include= val[index] + KnapsackMemo(capacity- wt[index], val, wt, dp, index+1);
}
// Exclude
int exclude= 0 + KnapsackMemo(capacity, val, wt, dp, index+1);
dp[capacity][index]= max(include, exclude);
return dp[capacity][index];
}
int Knapsack(int capacity, vector<int> &val, vector<int> &wt){
vector<vector<int>> dp(capacity+1, vector<int>(val.size() + 1, -1));
return KnapsackMemo(capacity, val, wt, dp, 0);
}
// Tabulation KnapSack Problem M2
int KnapsackTab(int capacity, vector<int> &val, vector<int> &wt){
int size= val.size();
vector<vector<int>> dp(capacity+1, vector<int> (val.size() + 1, -1));
// Known Values
// Setting all [rows][last] == 0 i.e Base Case
for(int i=0; i<capacity+1; i++){
dp[i][size]= 0;
}
// Tabulation
for(int i=0; i<capacity+1; i++){
// Known Value me daal diya
for(int j= size-1; j>=0; j--){
// Include
int include= INT_MIN;
if(wt[j] <= i){
// Value + max profit for that capacity
include= val[j] + dp[i - wt[j]][j+1];
}
// Exclude
int exclude= 0 + dp[i][j+1];
dp[i][j]= max(include, exclude);
}
}
return dp[capacity][0];
}
// Tabulation Space Optimization KnapSack Problem M3
int KnapsackTabSOM1(int capacity, vector<int> &val, vector<int> &wt){
// Row koi bhi ho sakti hai i.e capacity j & j+1 rahega i.e next prev
// Initially sare next=0 nai toh -1 daal jaega
// Known Values, SO
vector<int> next(capacity+1, 0);
vector<int> prev(capacity+1, -1);
// Tabulation
// Ulta kiya kyuki hum by val traverse kr rahe hai
for(int j= val.size()-1; j>=0; j--){
for(int i=0; i<capacity+1; i++){
// Include
int include= INT_MIN;
if(wt[j] <= i){
// Value + max profit for that capacity
include= val[j] + next[i-wt[j]];
}
// Exclude
int exclude= 0 + next[i];
prev[i]= max(include, exclude);
}
// Shifting
next= prev;
}
return next[capacity];
}
// Tabulation More Space Optimization KnapSack Problem M4
int KnapsackTabSOM2(int capacity, vector<int> &val, vector<int> &wt){
// Known Values, SO
vector<int> curr(capacity+1, 0);
// Tabulation
// Ulta kiya kyuki hum by val traverse kr rahe hai
for(int j= val.size()-1; j>=0; j--){
// i-wt[j] hoga i.e i se upar wale change honge & toh i ko change kr sakte hai
for(int i=capacity; i>=0; i--){
// Include
int include= INT_MIN;
if(wt[j] <= i){
// Value + max profit for that capacity
include= val[j] + curr[i-wt[j]];
}
// Exclude
int exclude= 0 + curr[i];
// Shifting
curr[i]= max(include, exclude);
}
}
return curr[capacity];
}
// Longest Common Subsequence
// Recursion Longest Common Subsequence Problem M1, TLE
int LCSRec(string &text1, string &text2, int i, int j){
// Base Case
if(i >= text1.length()){
return 0;
}
if(j >= text2.length()){
return 0;
}
int ans=0;
// Recursion Call & Processing
// Match
if(text1[i] == text2[j]){
ans = 1 + LCSRec(text1, text2, i+1, j+1);
}
// No Match
else{
// Exclude text 1
int exc1= 0 + LCSRec(text1, text2, i+1, j);
// Exclude text 2
int exc2= 0 + LCSRec(text1, text2, i, j+1);
// Longest Possible
ans= max(exc1, exc2);
}
return ans;
}
int LCSM1(string text1, string text2){
int i=0;
int j=0;
return LCSRec(text1, text2, i, j);
}
// Memoization Longest Common Subsequence Problem M2, TLE if text1 & text2 not passed by-ref
int LCSMemo(string &text1, string &text2, int i, int j, vector<vector<int>> &dp){
// Base Case
if(i >= text1.length()){
return 0;
}
if(j >= text2.length()){
return 0;
}
if(dp[i][j] != -1){
return dp[i][j];
}
// Memoization
// Match
if(text1[i] == text2[j]){
dp[i][j] = 1 + LCSMemo(text1, text2, i+1, j+1, dp);
}
// No Match
else{
// Exclude text 1
int exc1= 0 + LCSMemo(text1, text2, i+1, j, dp);
// Exclude text 2
int exc2= 0 + LCSMemo(text1, text2, i, j+1, dp);
// Longest Possible
dp[i][j]= max(exc1, exc2);
}
return dp[i][j];
}
int LCSM2(string text1, string text2){
int i=0;
int j=0;
vector<vector<int>> dp(text1.length()+1, vector<int>(text2.length()+1, -1));
return LCSMemo(text1, text2, i, j, dp);
}
// Tabulation Longest Common Subsequence Problem M3
int LCSTab(string &text1, string &text2){
vector<vector<int>> dp(text1.length()+1, vector<int>(text2.length()+1, 0));
// Know Values last ki hai toh ulta travel
// Text1 in rows and Text2 in cols
// Tabulation
for(int i= text1.length()-1; i>=0; i--){
for(int j= text2.length()-1; j>=0; j--){
// Match
if(text1[i] == text2[j]){
dp[i][j] = 1 + dp[i+1][j+1];
}
// No Match
else{
// i+1 and j+1 phele hi calculate ho gye honge as ginti ulti chal ri hai
dp[i][j]= 0 + max(dp[i+1][j], dp[i][j+1]);
}
}
}
return dp[0][0];
}
// Tabulation Space Optimization Longest Common Subsequence Problem M4
int LCSTabSO(string &text1, string &text2){
// SO
vector<int> next(text2.length()+1, 0);
vector<int> prev(text2.length()+1, 0);
// Text1 in rows and Text2 in cols
// Tabulation
for(int i= text1.length()-1; i>=0; i--){
for(int j= text2.length()-1; j>=0; j--){
// Match
if(text1[i] == text2[j]){
prev[j] = 1 + next[j+1];
}
// No Match
else{
prev[j]= 0 + max(next[j], prev[j+1]);
}
}
// Shifting
next= prev;
}
return next[0];
}
// Longest Palindromic Subsequence
// Recursion Longest Palindromic Subsequence Problem M1, TLE
int LPSRec(string &s, int start, int end){
// Base Case
if(start == end){
return 1;
}
if(start > end){
return 0;
}
int ans=0;
// Recursion Call & Processing
// Match
if(s[start] == s[end]){
// 2 plus kyuki 2 characters match kr rahe hai, single case is handled
ans= 2 + LPSRec(s, start+1, end-1);
}
// No Match
else{
int skipStart= 0 + LPSRec(s, start+1, end);
int skipEnd= 0 + LPSRec(s, start, end-1);
ans= max(skipStart, skipEnd);
}
return ans;
}
int LPSM1(string s){
int start= 0;
int end= s.length()-1;
return LPSRec(s, start, end);
}
// Memoization Longest Palindromic Subsequence Problem M2
int LPSMemo(string &s, int start, int end, vector<vector<int>> &dp){
// Base Case
if(start == end){
return 1;
}
if(start > end){
return 0;
}
if(dp[start][end] != -1){
return dp[start][end];
}
// Memoization
// Match
if(s[start] == s[end]){
dp[start][end]= 2 + LPSMemo(s, start+1, end-1, dp);
}
// No Match
else{
dp[start][end]= max(LPSMemo(s, start+1, end, dp), LPSMemo(s, start, end-1, dp));
}
return dp[start][end];
}
int LPSM2(string s){
int start= 0;
int end= s.length()-1;
// 1D-dp se isliye kaam nai chalega kyuki end-1-start && end-start-1 me overlapping ho jaegi
vector<vector<int>> dp(s.length(), vector<int>(s.length(), -1));
return LPSMemo(s, start, end, dp);
}
// Tabulation Longest Palindromic Subsequence Problem M3
int LPSTab(string &s){
int size= s.length();
vector<vector<int>> dp(size+1, vector<int>(size+1, 0));
// Known Values, start == end wale case
for(int i=0; i<size; i++){
dp[i][i]= 1;
}
// Ulta isliye kyuki woh values phele calculate krni hai
// Tabulation
for(int start= size-1; start>=0; start--){
for(int end=0; end<size; end++){
// Condition
if(start < end){
// Match
if(s[start] == s[end]){
dp[start][end]= 2 + dp[start+1][end-1];
}
// No Match
else{
dp[start][end]= max(dp[start+1][end], dp[start][end-1]);
}
}
}
}
return dp[0][size-1];
}
// Tabulation Space Optimization Longest Palindromic Subsequence Problem M4
int LPSTabSO(string &s){
int size= s.length();
vector<int> next(size, 0);
vector<int> prev(size, 0);
// Ulta isliye kyuki woh values phele calculate krni hai
// Tabulation
for(int start= size-1; start>=0; start--){
prev[start] = 1;
for(int end=0; end<size; end++){
if(start < end){
// Match
if(s[start] == s[end]){
prev[end]= 2 + next[end-1];
}
// No Match
else{
prev[end]= max(prev[end-1], next[end]);
}
}
}
next= prev;
}
return next[size-1];
}
// Edit Distance
// Memoization Edit Distance Problem M1
int EditDistanceMemo(string &word1, string &word2, int i, int j, vector<vector<int>> &dp){
// Base Case
if(i >= word1.length()){
return word2.length()-j;
}
if(j >= word2.length()){
return word1.length()-i;
}
if(dp[i][j] != -1){
return dp[i][j];
}
// Match
if(word1[i] == word2[j]){
dp[i][j]= 0+ EditDistanceMemo(word1, word2, i+1, j+1, dp);
}
// No Match
else{
// Insertion
// dp me first insert me jaega uske baad ke haar baar 3no options explore ho jaenge, deletion and replacing me nai krega
int insertion= 1+ EditDistanceMemo(word1, word2, i, j+1,dp);
// Deletion
int deletion= 1+ EditDistanceMemo(word1, word2, i+1, j, dp);
// Replacing
int replacing= 1+ EditDistanceMemo(word1, word2, i+1, j+1, dp);
dp[i][j]= min(insertion, min(deletion, replacing));
}
return dp[i][j];
}
int EditDistance(string word1, string word2) {
int i=0;
int j=0;
vector<vector<int>> dp(word1.length()+1, vector<int>(word2.length()+1, -1));
return EditDistanceMemo(word1, word2, i, j, dp);
}
// Tabulation Edit Distance Problem M2
int EditDistanceTab(string &word1, string &word2){
int size1=word1.length();
int size2=word2.length();
vector<vector<int>> dp(word1.length()+1, vector<int>(word2.length()+1, 0));
// Known Values, Using Base Cases
for(int row=0; row<size1; row++){
dp[row][size2]= size1-row;
}
for(int col=0; col<size2; col++){
dp[size1][col]= size2-col;
}
// Tabulation
for(int i= size1-1; i>=0; i--){
for(int j= size2-1; j>=0; j--){
// Match
if(word1[i] == word2[j]){
dp[i][j]= 0+ dp[i+1][j+1];
}
// No Match
else{
// Insertion
int insertion= 1+ dp[i][j+1];
// Deletion
int deletion= 1+ dp[i+1][j];
// Replacing
int replacing= 1+ dp[i+1][j+1];
dp[i][j]= min(insertion, min(deletion, replacing));
}
}
}
return dp[0][0];
}
// Tabulation Space Optimization Edit Distance Problem M3
int EditDistanceTabSO(string &word1, string &word2){
int size1=word1.length();
int size2=word2.length();
vector<int> next(size2+1, 0);
vector<int> prev(size2+1, 0);
// Known Values, Using Base Cases
for(int col=0; col<size2; col++){
next[col]= size2-col;
}
// Tabulation
for(int i= size1-1; i>=0; i--){
// Setting up the last value, column ki value
prev[size2] = size1 - i;
for(int j= size2-1; j>=0; j--){
// Match
if(word1[i] == word2[j]){
prev[j]= 0+ next[j+1];
}
// No Match
else{
// Insertion
int insertion= 1+ prev[j+1];
// Deletion
int deletion= 1+ next[j];
// Replacing
int replacing= 1+ next[j+1];
prev[j]= min(insertion, min(deletion, replacing));
}
}
// Shifting
next= prev;
}
return next[0];
}
// Longest Increasing Subsequence
// Recursion Longest Increasing Subsequence Problem M1
int LISRec(vector<int> &nums, int prev, int currIndex){
// Base Case
if(currIndex >= nums.size()){
return 0;
}
// Recursion Call & Processing
int include=0;
if(prev == -1 || nums[currIndex] > nums[prev]){
include= 1+ LISRec(nums, currIndex, currIndex+1);
}
int exclude= 0+ LISRec(nums, prev, currIndex+1);
int ans= max(include, exclude);
return ans;
}
int LISM1(vector<int> &nums){
int prev= -1;
int currIndex= 0;
return LISRec(nums, prev, currIndex);
}
// Memoization Longest Increasing Subsequence Problem M2
int LISMemo(vector<int> &nums, int prev, int currIndex, vector<vector<int>> &dp){
// Base Case
if(currIndex >= nums.size()){
return 0;
}
// Index shifting i.e all values are stored in the next column
if(dp[currIndex][prev+1] != -1){
return dp[currIndex][prev+1];
}
// Memoization
int include=0;
if(prev == -1 || nums[currIndex] > nums[prev]){
include= 1+ LISMemo(nums, currIndex, currIndex+1, dp);
}
int exclude= 0+ LISMemo(nums, prev, currIndex+1, dp);
dp[currIndex][prev+1]= max(include, exclude);
return dp[currIndex][prev+1];
}
int LISM2(vector<int> &nums){
int prev= -1;
int currIndex= 0;
vector<vector<int>> dp(nums.size()+1, vector<int>(nums.size()+1, -1));
return LISMemo(nums, prev, currIndex, dp);
}
// Tabulation Longest Increasing Subsequence Problem M3
int LISTab(vector<int> &nums){
vector<vector<int>> dp(nums.size()+1, vector<int>(nums.size()+1, 0));
// Tabulation
for(int i= nums.size()-1; i>=0; i--){
for(int j=i-1; j>=-1; j--){
int include= 0;
if(j == -1 || nums[i] > nums[j]){
// Index Shifting include= 1+ dp[i+1][i]
// dp[curr][curr]
include= 1+ dp[i+1][i+1];
}
// Index Shifting exclude= 0+ dp[i+1][j]
int exclude= 0 + dp[i+1][j+1];
// dp[i+1][j]
dp[i][j+1]= max(include, exclude);
}
}
// dp[0][-1]
return dp[0][0];
}
// Tabulation Space Optimization Longest Increasing Subsequence Problem M4
int LISTabSO(vector<int> &nums){
int size= nums.size();
vector<int> next(size+1, 0);
vector<int> prev(size+1, 0);
for(int i= nums.size()-1; i>=0; i--){
for(int j=i-1; j>=-1; j--){
int include= 0;
if(j == -1 || nums[i] > nums[j]){
// Index Shifting include= 1+ dp[i+1][i]
// dp[curr][curr]
include= 1+ next[i+1];
}
// Index Shifting exclude= 0+ dp[i+1][j]
int exclude= 0 + next[j+1];
// dp[i+1][j]
prev[j+1]= max(include, exclude);
}
// Shifting
next= prev;
}
// dp[0][-1]
return next[0];
}
// Binary Search Longest Increasing Subsequence Problem M5
int LISbs(vector<int> &nums){
vector<int> ans;
ans.push_back(nums[0]);
for(int i=1; i<nums.size(); i++){
if(nums[i] > ans.back()){
ans.push_back(nums[i]);
}
else{
// Closest greater no.
int index= lower_bound(ans.begin(), ans.end(), nums[i]) - ans.begin();
// Replace
ans[index]= nums[i];
}
}
return ans.size();
}
// Maximum Height by Stacking Cuboids
// Tabulation Space Optimization Maximum Height by Stacking Cuboids Problem
bool CuboidStackCheck(vector<int> &a, vector<int> &b){
// Length Width Height
if(a[0] >= b[0] && a[1] >= b[1] && a[2] >= b[2]){
return true;
}
return false;
}
int CuboidStackTabSO(vector<vector<int>> &cuboids){
vector<int> next(cuboids.size()+1, 0);
vector<int> prev(cuboids.size()+1, 0);
for(int i=cuboids.size()-1; i>=0; i--){
for(int j=i-1; j>=-1; j--){
int include= 0;
if(j == -1 || CuboidStackCheck(cuboids[i], cuboids[j]) ){
// Index Shifting include= 1+ dp[i+1][i]
// dp[curr][curr]
include= cuboids[i][2] + next[i+1];
}
// Index Shifting exclude= 0+ dp[i+1][j]
int exclude= 0 + next[j+1];
// dp[i+1][j]
prev[j+1]= max(include, exclude);