記一次 TX_Company 的測試開發一面面試題目 (演算法題:第 77 題-爬樓梯)

MmoMartin發表於2020-06-14

進入騰訊會議開始:

1、打招呼,介紹自己【自己的工作內容與技能】
2、直接進入主題:
兩道演算法題:
1、寫一個樹的中序排序演算法。【這道題固定模式,沒啥好玩的,自己去熟悉就可以了,沒有下面例子題目有趣】

2、是Leecode的第70. 爬樓梯面試題目:
假設你正在爬樓梯。需要 n 階你才能到達樓頂。
每次你可以爬 1 或 2 個臺階。你有多少種不同的方法可以爬到樓頂呢?
注意:給定 n 是一個正整數。

我的解題思路:

from itertools import permutations
def step_all(n):
total = 0
"""
:param n: 總檯階,每次只走1或2,組合有多少種

:return: all
"""

if n == 1:
return 1
elif n == 2:
return 2
elif n >= 3:
if n % 2 == 0:
step_min = int(n/2) # 最小的step登頂
for i in range(step_min+1, n):
len_i = i # 長度
count_two = (step_min - (len_i - step_min))
count_one = len_i - count_two
list_step = [2 for x in range(count_two)]
list_step.extend([1 for y in range(count_one)])
li_per = list(permutations(list_step))
count = len(set(li_per))
total += count
return total+2
elif n % 2 == 1:
total = 0
step_min = int((n-1) / 2) + 1 # 最小的step登頂,step-min2的個數 3:2 4:1 5:0
for i in range(step_min+1, n):
len_i = i # 長度
count_two = (step_min - (len_i-(step_min-1)))
print(count_two)
count_one = len_i - count_two
list_step = [2 for x in range(count_two)]
list_step.extend([1 for y in range(count_one)])
li_per = list(permutations(list_step))
count = len(set(li_per))
print(count)
total += count
print(total)
step_min_list = [2 for z in range(step_min-1)]
step_min_list.extend([1 for i in range(1)])
li_per = list(permutations(step_min_list))
step_min_list_count = len(set(li_per))
total = total + step_min_list_count + 1
return total

上述的程式碼在1-15之間的執行是沒問題的,所以便提交到Leecode,發現報錯。
然後便換一種方式了,原理不變,藉助數學組合公式並去重得出結果【核心是:對[1,1,1,2,2,1,1]類似的資料進行去重組合】

def step_all(n):
def mutil_sum(start_x, end_x=None):
sum_total = 1
if end_x:
print(end_x)
for i in range(start_x, start_x - end_x, -1):
sum_total *= i
return sum_total
for i in range(1, start_x + 1):
sum_total *= i
return sum_total
if n == 1:
return 1
elif n == 2:
return 2
elif n >= 3:
if n % 2 == 0:
total = 0
min_len = int((n / 2))
count_two = min_len
count_one = 0
for i in range(min_len, n):
print("只需%s次爬上樓頂"%(count_two + count_one))
print("2的個數:%s" % count_two)
print("1的個數:%s" % count_one)
if count_two >= count_one:
type_sum = mutil_sum((count_one+count_two), count_two) / mutil_sum(count_two)
total += type_sum
print("只需%s次爬上樓頂的組合數為:%s"%((count_two + count_one), type_sum))
else:
type_sum = mutil_sum((count_one + count_two), count_one) / mutil_sum(count_one)
total += type_sum
print("只需%s次爬上樓頂的組合數為:%s" % ((count_two + count_one), type_sum))
count_two -= 1
count_one += 2
print("只需%s次爬上樓頂" % (count_two + count_one))
print("2的個數:%s" % count_two)
print("1的個數:%s" % count_one)
if count_two >= count_one:
print("%s >= %s"%(count_two, count_one))
type_sum = (mutil_sum((count_one+count_two), count_two)) / mutil_sum(count_two)
total += type_sum
print("只需%s次爬上樓頂的組合數為:%s" % ((count_two + count_one), type_sum))
else:
type_sum = (mutil_sum((count_one+count_two), count_two)) / mutil_sum(count_one)
total += type_sum
print("只需%s次爬上樓頂的組合數為:%s" % ((count_two + count_one), type_sum))
return total
if n % 2 == 1:
total = 0
min_len = int(((n - 1) / 2)) + 1
count_two = min_len - 1
count_one = 1
for i in range(min_len, n):
print("只需%s次爬上樓頂"%(count_two + count_one))
print("2的個數:%s" % count_two)
print("1的個數:%s" % count_one)
if count_two >= count_one:
type_sum = (mutil_sum((count_one+count_two), count_two)) / mutil_sum(count_two)
total += type_sum
print("只需%s次爬上樓頂的組合數為:%s"%((count_two + count_one), type_sum))
else:
type_sum = (mutil_sum((count_one+count_two), count_one)) / mutil_sum(count_one)
total += type_sum
print("只需%s次爬上樓頂的組合數為:%s" % ((count_two + count_one), type_sum))
count_two -= 1
count_one += 2
print("只需%s次爬上樓頂" % (count_two + count_one))
print("2的個數:%s" % count_two)
print("1的個數:%s" % count_one)
if count_two >= count_one:
type_sum = (mutil_sum((count_one+count_two), count_two)) / mutil_sum(count_two)
total += type_sum
print("只需%s次爬上樓頂的組合數為:%s" % ((count_two + count_one), type_sum))
else:
type_sum = (mutil_sum((count_one+count_two), count_one)) / mutil_sum(count_one)
total += type_sum
print("只需%s次爬上樓頂的組合數為:%s" % ((count_two + count_one), type_sum))
return total

執行結果OK,程式碼的執行效率底下,需要進行優化,目前只是能實現功能而已。可惜當時沒時間。沒去發現規律。不過我覺得這種更好玩,比那種直接寫二叉樹啥的固定模式好玩。用例子來描述演算法,這方式值得提倡。

相關文章