有M层楼和N个鸡蛋,要找到扔下鸡蛋而鸡蛋不碎的最低楼层(也称为“临界楼层”),最少尝试多少次就一定能找到这个“最低楼层”?
这个题意其实并不那么直观,有以下的几个注意点:所以,以第X层为初始楼层的最坏情况就是以上二者的最大值,即:
1 + Max(F(M-X, N), F(X-1, N-1)) (当X为初始楼层)
因为每层楼都可能作为初始楼层来扔鸡蛋,所以,实际上存在某一楼层为初始楼层,其尝试次数可以达到最少。也就是说:
F(M, N) = Min(1 + Max(F(M-X, N), F(X-1, N-1))) # X 属于 [1, M]
这样,就可以写程序了。当找到尝试次数的最小值的时候,也就找到了初始楼层。这就为我们反推出扔鸡蛋的方法也提供了方法。程序如下(对于100层楼+2个鸡蛋,当然,楼的层数和鸡蛋的个数是可以设置的):
cache = dict()
floors_result = dict() # each floor's max trying number
def f(m, n, first_level=False):
if m == 0:
return 0
if m == 1:
return 1
if n == 1:
return m
m_cache = cache.get(m)
if m_cache and m_cache.get(n):
return cache[m][n]
result_list = list()
for i in range(m):
i += 1
result = max(f(m-i, n), f(i-1, n-1)) + 1
result_list.append(result)
if first_level:
floors_result[i] = result
result = min(result_list)
if not cache.get(m):
cache[m] = dict()
cache[m][n] = result
return result
def main():
m = 100
n = 2
result = f(m, n, True)
for flr, res in floors_result.items():
if res == result:
first_floor = flr
break
print("F(%s, %s) = %s, first floor is %s" % (m, n, result, first_floor))
print("{}".format(floors_result))
main()
程序的打印结果如下:
$ python 1.py
F(100, 2) = 14, first floor is 9
{1: 15, 2: 15, 3: 15, 4: 15, 5: 15, 6: 15, 7: 15, 8: 15, 9: 14, 10: 14, 11: 14, 12: 14, 13: 14, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, 30: 30, 31: 31, 32: 32, 33: 33, 34: 34, 35: 35, 36: 36, 37: 37, 38: 38, 39: 39, 40: 40, 41: 41, 42: 42, 43: 43, 44: 44, 45: 45, 46: 46, 47: 47, 48: 48, 49: 49, 50: 50, 51: 51, 52: 52, 53: 53, 54: 54, 55: 55, 56: 56, 57: 57, 58: 58, 59: 59, 60: 60, 61: 61, 62: 62, 63: 63, 64: 64, 65: 65, 66: 66, 67: 67, 68: 68, 69: 69, 70: 70, 71: 71, 72: 72, 73: 73, 74: 74, 75: 75, 76: 76, 77: 77, 78: 78, 79: 79, 80: 80, 81: 81, 82: 82, 83: 83, 84: 84, 85: 85, 86: 86, 87: 87, 88: 88, 89: 89, 90: 90, 91: 91, 92: 92, 93: 93, 94: 94, 95: 95, 96: 96, 97: 97, 98: 98, 99: 99, 100: 100}
由上可知,
对于100层楼+2个鸡蛋,首次可以在第9层至第14层中的任何一层扔都可以!最坏情况下需要扔14次鸡蛋才能确定临界楼层。
于是,以上就确定了扔鸡蛋的方法了。
那么,对于100层楼+2个鸡蛋,可以使用以下程序得到“一直摔不碎第一个鸡蛋的情况下的所有尝试楼层”,故以下程序不适用大于2个鸡蛋的情况,但对于大于2个鸡蛋的情况,通过上一段描述的方法,也可以得到答案。
cache = dict()
floors_result = dict() # each floor's max trying number
def f(m, n, first_level=False):
if m == 0:
return 0
if m == 1:
return 1
if n == 1:
return m
m_cache = cache.get(m)
if m_cache and m_cache.get(n):
return cache[m][n]
result_list = list()
for i in range(m):
i += 1
result = max(f(m-i, n), f(i-1, n-1)) + 1
result_list.append(result)
if first_level:
floors_result[i] = result
result = min(result_list)
if not cache.get(m):
cache[m] = dict()
cache[m][n] = result
return result
def main():
m = 100
n = 2
floor_list = list()
result = f(m, n, True)
floor = -1
for flr, res in floors_result.items():
if res == result:
floor = flr
break
floor_list.append(floor)
while floor <= m - 1:
result = f(m-floor, n)
floor += result
floor_list.append(floor)
print(floor_list)
main()
执行结果是:
[9, 22, 34, 45, 55, 64, 72, 79, 85, 90, 94, 97, 99, 100]
我们平常搜索“高楼扔鸡蛋”,看到解答一般都是100层+2个鸡蛋对应的都是从14层开始扔,最多14次找到临界楼层。但是由以上的分析可知,其实从9-14层的任何一层,都可以是最多14次找到临界楼层。
(完)