習題19:

Let's say that the 'slide down' is a sum of consecutive numbers from the top to the bottom of the pyramid. As you can see, the longest 'slide down' is 3 + 7 + 4 + 9 = 23

Your task is to write a function longestSlideDown (in ruby: longest_slide_down) that takes a pyramid representation as argument and returns its' longest 'slide down'.

找出一條從塔尖往下滑的通道,這條通道所有數字相加是最大的。

   /3/
  \7\ 4 
 2 \4\ 6 
8 5 \9\ 3

def longest_slide_down(pyramid):
# TODO: write some code...
# 從0,0開始
l = []
length = len(pyramid)
def steps(x=0, y=0, step=0):
step += pyramid[x][y]
if x == length - 1:
l.append(step)
else:
steps(x+1, y+1, step)
steps(x+1, y, step)
steps()
return max(l)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容