Skip to content

Commit 6bdb62b

Browse files
committed
🚀 30-Aug-2020
1 parent 0f46694 commit 6bdb62b

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

competitive programming/leetcode/645. Set Mismatch.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,11 @@ class Solution {
5757
return v;
5858
}
5959
};
60+
61+
62+
// Another approach is to check if nums[abs[nums]-1] is negative or not, if it's negative then number is repeating
63+
/* for missing
64+
for(int i = 0; i<n; i++)
65+
if(nums]>0)
66+
missing = i+1;
67+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
You are given K eggs, and you have access to a building with N floors from 1 to N.
2+
3+
Each egg is identical in function, and if an egg breaks, you cannot drop it again.
4+
5+
You know that there exists a floor F with 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.
6+
7+
Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).
8+
9+
Your goal is to know with certainty what the value of F is.
10+
11+
What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?
12+
13+
14+
15+
Example 1:
16+
17+
Input: K = 1, N = 2
18+
Output: 2
19+
Explanation:
20+
Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
21+
Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1.
22+
If it didn't break, then we know with certainty F = 2.
23+
Hence, we needed 2 moves in the worst case to know what F is with certainty.
24+
Example 2:
25+
26+
Input: K = 2, N = 6
27+
Output: 3
28+
Example 3:
29+
30+
Input: K = 3, N = 14
31+
Output: 4
32+
33+
34+
Note:
35+
36+
1 <= K <= 100
37+
1 <= N <= 10000
38+
39+
40+
41+
42+
43+
44+
// DP (TLE)
45+
46+
class Solution {
47+
public:
48+
// k=e=egg n=f=floor
49+
int superEggDrop(int e, int f) {
50+
int dp[e+1][f+1];
51+
52+
for(int i=0;i<e+1;i++){
53+
dp[i][0]=0; // 0 floors
54+
dp[i][1]=1; // 1 floor
55+
}
56+
for(int j=1;j<f+1;j++){
57+
dp[0][j]=0; // 0 egg
58+
dp[1][j]=j;
59+
}
60+
61+
for(int i=2;i<e+1;i++){
62+
for(int j=2;j<f+1;j++){
63+
dp[i][j]=INT_MAX;
64+
for(int x=1;x<=j;x++){
65+
int tmp=1+max(dp[i-1][x-1], dp[i][j-x]);
66+
if(tmp<dp[i][j]) dp[i][j]=tmp;
67+
}
68+
}
69+
}
70+
return dp[e][f];
71+
}
72+
};
73+
74+
75+
76+
77+
// Recursive (Memoized) (TLE)
78+
79+
class Solution {
80+
public:
81+
int dp[101][10001]; // according to the constraints
82+
83+
int eggDrop(int e, int f, int dp[][10001]){
84+
if(e==1 || e==0) return f;
85+
if(f==0 || f==1) return f;
86+
87+
if(dp[e][f]!=-1) return dp[e][f];
88+
89+
int mini=INT_MAX;
90+
int egb, egnb;
91+
for(int k=1;k<=f;k++){
92+
if(dp[e-1][k-1]!=-1) egb=dp[e-1][k-1];
93+
else {
94+
egb=eggDrop(e-1, k-1, dp);
95+
dp[e-1][k-1]=egb;
96+
}
97+
if(dp[e][f-k]!=-1) egnb=dp[e][f-k];
98+
else {
99+
egnb=eggDrop(e, f-k, dp);
100+
dp[e][f-k]=egnb;
101+
}
102+
103+
int tmp = 1 + max(egb, egnb);
104+
if(tmp<mini) mini=tmp;
105+
}
106+
return dp[e][f]=mini;
107+
}
108+
109+
110+
// k=e=eggs n=f=floor
111+
int superEggDrop(int e, int f) {
112+
memset(dp, -1, sizeof(dp));
113+
return eggDrop(e, f, dp);
114+
}
115+
};
116+

0 commit comments

Comments
 (0)