Skip to content

Commit da635fc

Browse files
author
shiv
committed
added egg drop puzzle with memoized solution
1 parent cb35bba commit da635fc

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

.egg_drop.py.swp

12 KB
Binary file not shown.

egg_drop.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SHIV's code for egg dropping
2+
3+
'''
4+
1
5+
2 10
6+
'''
7+
8+
tn = int(input())
9+
10+
egg_dict = {}
11+
def egger(floor, egg):
12+
if floor == 0 or floor == 1:
13+
return floor
14+
if egg == 1:
15+
return floor
16+
eggdr = []
17+
for i in range(1, floor+1):
18+
if (i-1, egg-1) in egg_dict:
19+
a = egg_dict[i-1, egg-1]
20+
else:
21+
a = egger(i-1, egg-1)
22+
egg_dict[i-1, egg-1] = a
23+
if (floor-i, egg) in egg_dict:
24+
b = egg_dict[floor-i, egg]
25+
else:
26+
b = egger(floor-i, egg)
27+
egg_dict[floor-i, egg] = b
28+
eggdr.append(max(a, b))
29+
return min(eggdr)+1
30+
31+
for i in range(tn):
32+
egg_dict = {}
33+
egg, floor = list(map(int, input().split()))
34+
print(egger(floor, egg))

ip.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
2
2-
YX X XXY
3-
XY X XXY
1+
1
2+
2 10

0 commit comments

Comments
 (0)