forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegg_drop_all.cpp
285 lines (161 loc) · 6.02 KB
/
egg_drop_all.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
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
You are given k identical eggs and you have access to a building with n floors labeled from 1 to n.
You know that there exists a floor f where 0 <= f <= n such that any egg dropped at
a floor higher than f will break, and any egg dropped at or below floor f will not break.
Each move, you may take an unbroken egg and drop it from any floor x (where 1 <= x <= n).
If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.
Return the minimum number of moves that you need to determine with certainty what the value of f is.
Example 1:
Input: k = 1, n = 2
Output: 2
Explanation:
Drop the egg from floor 1. If it breaks, we know that f = 0.
Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1.
If it does not break, then we know f = 2.
Hence, we need at minimum 2 moves to determine with certainty what the value of f is.
Example 2:
Input: k = 2, n = 6
Output: 3
Example 3:
Input: k = 3, n = 14
Output: 4
Constraints:
1 <= k <= 100
1 <= n <= 104
// Recursive (TLE)
class Solution {
public:
// e=eggs f=floor
int superEggDrop(int e, int f) {
if (e == 1 || e == 0) return f;
if (f == 0 || f == 1) return f;
int mini = INT_MAX;
for (int k = 1; k <= f; k++){
int tmp = 1 + max(superEggDrop(e - 1, k - 1), superEggDrop(e, f - k));
if (tmp < mini) mini = tmp;
}
return mini;
}
};
// Recursion Memoization (TLE)
class Solution {
public:
vector <vector<int>> dp;
int eggDrop(int e, int f){
if (e == 1 || e == 0) return f;
if (f == 0 || f == 1) return f;
if (dp[e][f] != -1) return dp[e][f];
int mini = INT_MAX;
int egb, egnb;
for (int k = 1; k <= f; k++){
if (dp[e - 1][k - 1] != -1) egb = dp[e - 1][k - 1];
else {
egb = eggDrop(e - 1, k - 1);
dp[e - 1][k - 1] = egb;
}
if (dp[e][f - k] != -1) egnb = dp[e][f - k];
else {
egnb = eggDrop(e, f - k);
dp[e][f - k] = egnb;
}
int tmp = 1 + max(egb, egnb);
if(tmp < mini) mini = tmp;
}
return dp[e][f] = mini;
}
// e = egg, f = floor
int superEggDrop(int e, int f) {
dp.resize(e + 1, vector <int> (f + 1, -1));
return eggDrop(e, f);
}
};
// Recursive Memoization + Binary Search (Accepted)
class Solution {
public:
vector <vector<int>> dp;
int eggDrop(int e, int f){
if (e == 1 || e == 0) return f;
if (f == 0 || f == 1) return f;
if (dp[e][f] != -1) return dp[e][f];
int mini = INT_MAX;
int start = 1, end = f;
while (start <= end) {
int mid = start + (end - start) / 2;
int egb = eggDrop(e - 1, mid - 1); //egg broken check for down floors of mid (left)
int egnb = eggDrop(e, f - mid); // not broken check for up floors of mid (right)
int tmp = 1 + max(egb, egnb); //store max of both
if (egb < egnb) { // since right is more than left and we need more in worst case
start = mid + 1; // so start = mid + 1; to gain more temp for worst case : upward
} else {
end = mid - 1; //left > right so we will go downward
}
mini = min(mini, tmp);
}
return dp[e][f] = mini;
}
int superEggDrop(int e, int f) {
dp.resize(e + 1, vector <int> (f + 1, -1));
return eggDrop(e, f);
}
};
// DP (TLE)
class Solution {
public:
int superEggDrop(int e, int f) {
int dp[e + 1][f + 1];
for (int i = 0; i < e + 1; i++){
dp[i][0] = 0; // 0 floors
dp[i][1] = 1; // 1 floor
}
for (int j = 1; j < f + 1; j++){
dp[0][j] = 0; // 0 egg
dp[1][j] = j;
}
for (int i = 2; i < e + 1; i++){
for (int j = 2; j < f + 1; j++){
dp[i][j] = INT_MAX;
for( int x = 1; x <= j; x++){
int tmp = 1 + max(dp[i - 1][x - 1], dp[i][j - x]);
if (tmp < dp[i][j]) dp[i][j] = tmp;
}
}
}
return dp[e][f];
}
};
// DP + Binary Search (Accepted)
class Solution {
public:
int superEggDrop(int e, int f) {
if (e == 1 || e == 0) return f;
if (f == 0 || f == 1) return f;
int dp[e + 1][f + 1];
for (int i = 0; i < e + 1; i++){
dp[i][0] = 0; // 0 floors
dp[i][1] = 1; // 1 floor
}
for (int j = 1; j < f + 1; j++){
dp[0][j] = 0; // 0 egg
dp[1][j] = j;
}
for (int i = 2; i < e + 1; i++){
for(int j = 2; j < f + 1; j++){
int mini = INT_MAX;
int start = 1, end = j;
while (start <= end) {
int mid = start + (end - start) / 2;
int egb = dp[i - 1][mid - 1]; //egg broken check for down floors of mid (left)
int egnb = dp[i][j - mid]; // not broken check for up floors of mid (right)
int tmp = 1 + max(egb, egnb); //store max of both
if (egb < egnb) { // since right is more than left and we need more in worst case
start = mid + 1; // so start = mid + 1; to gain more temp for worst case : upward
} else {
end = mid - 1; //left > right so we will go downward
}
mini = min(mini, tmp);
}
dp[i][j] = mini;
}
}
return dp[e][f];
}
};