习题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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容