-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp_algos.cpp
234 lines (200 loc) · 7.03 KB
/
dp_algos.cpp
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
#include <bits/stdc++.h>
using namespace std;
class DpUtills {
/*
** If all subproblems must be solved at least once,
** a bottom-up dynamic-programming algorithm usually outperforms a top-down memoized algorithm
** by a constant factor.
** If some subproblems need not to be solved at all, memoization will be better than DP,
** else in general DP outperforms memo due to less overhead of call stack.
*/
/*
** MEMOIZATION TEMPLATE
datatype recur_func(n,r){
secure base cases:
return c;
check if value in memory:
return it.
x= INT_MAX/MIN
for loop:
x= min/max(recur_func(b,c), recur_func(d,e));
else return cache[n][r]= x;
}
*/
private:
/*
** generates a string adding chars at the start and end of original string
** that do not exist in original string
** adds a char again not part of original string, b/w every char
*/
string getManipualtedString(string s) {
string res = "$";
for (int i=0; i<s.size(); i++) {
res+= "#";
res+= s[i];
}
res+= "#";
res+= "@";
return res;
}
public:
/*
** manacher's algorithm
*/
string longestPalindromeSubString(string s) {
int n = s.size();
if (n<2) {
return s;
}
string TEST = getManipualtedString(s);
int m = TEST.size();
vector<int> dp = vector<int>(m, 0);
/*
** here we maintain center and number of elements
** which are same on the left and right of center
** at any point the palindrome is one with greatest r
** (so that for most i >c, we have pre computed values)
*/
int cur = 0, r= 0;
// these are final values of center and r in TEST string
int resC = 0, resL = 0;
for (int i = 2; i<m; i++) {
if (i<r) {
dp[i] = min(dp[2*cur - i], r-i);
}
// above ^^ initialization of dp[i], makes it possible not to compute several values
while (i - dp[i] - 1 >=0 && i + dp[i] + 1 < m && TEST[i + dp[i] + 1] == TEST[i - dp[i] - 1]) {
dp[i]++;
}
if (i + dp[i] > r) {
cur = i;
r = i + dp[i];
}
if (dp[i] > resL) {
resL = dp[i];
resC= i;
}
}
return s.substr((resC-1)/2 - resL/2, resL);
}
/*
** LIS
** n2 sol
*/
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
if (n < 2) {
return n;
}
int res = 1;
vector<int> dp = vector<int>(n, 1);
for (int i = 1; i<n; i++) {
for (int j=0; j<i; j++) {
if (nums[i] > nums[j]) {
dp[i] = max(dp[i], 1+dp[j]);
}
}
res = max(res, dp[i]);
}
return res;
}
/*
** with bin search lesser-than-or-equal-to variant
** at every index generate the LIS till that index with smallest numbers possible
** O(n) space
** O(nlog(n)) time
*/
int lengthOfLISBinarySearch (vector<int>& nums) {
int n= nums.size();
if (n<2) {
return n;
}
vector<int> lengths;
lengths.push_back(nums[0]);
for (int i=1; i<n; i++) {
// bin search
int pos = getLesserItem(lengths, 0, lengths.size()-1, nums[i]);
if(lengths[pos] >= nums[i]) {
lengths[pos] = nums[i];
} else if (lengths.size() > pos+1) {
lengths[pos+1] = nums[i];
} else {
lengths.push_back(nums[i]);
}
}
return lengths.size();
}
/*
** longest palindromic subsequence lps
** using O(n) space, O(n2) time
*/
int longestPalindromeSubseq(string s) {
int n = s.size();
if (n < 2) {
return n;
}
vector<int> dp = vector<int>(n, 0);
// before each iteration dp[j] contains length of lps bw [j, i-1];
for (int i=0; i<n; i++) {
int backup = 0;
dp[i] = 1;
for (int j = i-1; j>=0; j--) {
int newBackup = dp[j]; // contains lps [j, i-1]; this will become backup for [j-1, i] (i.e next iteration of j)
if (s[i] == s[j]) {
dp[j] = 2 + backup;
} else {
dp[j] = max(newBackup, dp[j+1]); // max of lps [j, i-1] and lps [j+1, i];
}
backup = newBackup;
}
}
return dp[0];
}
/*
** using O(n) space, O(n2) time
*/
int longestCommonSubsequence(string text1, string text2) {
int n = text1.size();
int m = text2.size();
if (n ==0 || m==0) {
return 0;
}
vector<int> dp = vector<int>(n, 0);
for (int j =0; j<m; j++) {
int backup = dp[0];
dp[0] = text1[0] == text2[j] || backup ? 1 : 0;
// pay attention to ^^ this initialization of dp[0]; || backup
for (int i = 1; i<n; i++) {
int newBackup = dp[i];
if (text1[i] == text2[j]) {
dp[i] = 1 + backup;
} else {
dp[i] = max(dp[i], dp[i-1]);
}
backup = newBackup;
}
}
return dp[n-1];
}
// kadane's algo
int maxSubArray(vector<int>& nums) {
int n = nums.size();
if ( n == 0) {
return 0;
}
long long res = nums[0];3
long long cur = 0;
for (int i=0; i<n; i++) {
/*
** if all nums are negative, we are comparing individual num with res,
** since cur = 0 at the start of every iteration
*/
cur+= nums[i];
res = max(res, cur);
if (cur < 0) {
cur = 0;
}
}
return res;
}
};