Golden Pyramid
Golden Pyramid
Our Robo-Trio need to train for future journeys and treasure hunts. Stephan has built a special flat model of a pyramid. Now the robots can train for speed gold running. They start at the top of the pyramid and must collect gold in each room, choose to take the left or right path and continue down to the next level. To optimise their gold runs, Stephan need to know the maximum amount of gold that can be collected in one run.
Consider a tuple of tuples in which the first tuple has one integer and each consecutive tuple has one more integer then the last. Such a tuple of tuples would look like a triangle. You should write a program that will help Stephan find the highest possible sum on the most profitable route down the pyramid. All routes down the pyramid involve stepping down and to the left or down and to the right.
Tips: Think of each step down to the left as moving to the same index location or to the right as one index location higher. Be very careful if you plan to use recursion here.
Input: A pyramid as a tuple of tuples. Each tuple contains integers.
Output: The maximum possible sum as an integer.
题目大义: 数字金字塔, 找出从顶部到底部, 路径的最大和(即这条路径上的数字相加的和为最大)
经典的DP问题D[i][j] = max(D[i - 1][j], D[i - 1][j - 1]) + Pyramid[i][j]
def count_gold(pyramid):
"""
Return max possible sum in a path from top to bottom
""" step = len(pyramid) sub_sum = [[0 for row in range(0, step)] for col in range(0, step)] sub_sum[0][0] = pyramid[0][0] for i in range(0, step):
for j in range(0, i + 1):
if i >= 1:
if j >= 1 and sub_sum[i - 1][j - 1] + pyramid[i][j] > sub_sum[i][j]:
sub_sum[i][j] = sub_sum[i - 1][j - 1] + pyramid[i][j] if sub_sum[i - 1][j] + pyramid[i][j] > sub_sum[i][j]:
sub_sum[i][j] = sub_sum[i - 1][j] + pyramid[i][j] max_sum = 0
for each in sub_sum[step - 1][:step]:
if each > max_sum:
max_sum = each #replace this for solution
return max_sum
其实这份代码类似C语言的实现
观摩zero_loss的python实现
def count_gold(pyramid):
py = [list(i) for i in pyramid]
for i in reversed(range(len(py)-1)):
for j in range(i+1):
py[i][j] +=(max(py[i+1][j], py[i+1][j+1])) return py[0][0]
这个实现是从下往上走, 非常简洁;
Golden Pyramid的更多相关文章
- [Math]PHI, the golden ratio
PHI, the golden ratio 黄金分割比 转载自 http://paulbourke.net/miscellaneous/miscnumbers/ 1. Definition 将一个线段 ...
- CF 676B Pyramid of Glasses[模拟]
B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Spatial pyramid pooling (SPP)-net (空间金字塔池化)笔记(转)
在学习r-cnn系列时,一直看到SPP-net的身影,许多有疑问的地方在这篇论文里找到了答案. 论文:Spatial Pyramid Pooling in Deep Convolutional Net ...
- 论文笔记之:Deep Generative Image Models using a Laplacian Pyramid of Adversarial Networks
Deep Generative Image Models using a Laplacian Pyramid of Adversarial Networks NIPS 2015 摘要:本文提出一种 ...
- Why The Golden Age Of Machine Learning is Just Beginning
Why The Golden Age Of Machine Learning is Just Beginning Even though the buzz around neural networks ...
- C Golden gun的巧克力
Time Limit:1000MS Memory Limit:65535K 题型: 编程题 语言: 无限制 描述 众所周知,13级有尊大神Golden gun,人称根叔,简称金枪!众立志进校队的 ...
- 10 Golden Rules of Project Risk Management
The benefits of risk management in projects are huge. You can gain a lot of money if you deal with u ...
- The golden ratio: 1.618
http://www.chinaz.com/design/2015/1109/467968_2.shtml The golden ratio: 1.618 a/b=b/(a+b) The Fibona ...
- codeforces 676B B. Pyramid of Glasses(模拟)
题目链接: B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- 第24讲 UI_布局 之帧布局 表格布局 绝对布局
第24讲 UI_布局 之帧布局 表格布局 绝对布局 3. FrameLayout(帧布局) 帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排序,后一个组件总会将前一个组件所覆盖,除非最后一 ...
- Python安装MySQLdb并连接MySQL数据库
当然了,前提是你已经安装了Python和MySQL.我的Python是2.6版本的. Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/fo ...
- WHY IE AGAIN? - string.charAt(x) or string[x]?
近期今天在写一个"删除字符串中反复字符串"的函数,代码例如以下: 开门见山,重点 string.charAt(index) 取代 string[index] function re ...
- 事关Animation Tree的工作随笔(二)
上回说到,游戏项目中客观会遇到逻辑状态的复杂性和动画状态的单一性之间的矛盾,那么Animation Tree是如何解决这个问题的呢? 这又需要引入一个定律:就是逻辑状态无论有多么复杂,但一套逻辑状态组 ...
- c++11 NULL、0、nullptr
C的NULL 在C语言中,我们使用NULL表示空指针,也就是我们可以写如下代码: int *i = NULL;foo_t *f = NULL; 实际上在C语言中,NULL通常被定义为如下: #de ...
- DOM事件处理程序-事件对象-键盘事件
事件流: 事件流--描述的是从页面中接受事件的顺序 IE ---事件冒泡流:即事件最开始由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播至最不具体的那个节点(文档). Netsc ...
- IL(Intermediate Language)
释义: IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不是CPU ...
- NVelocity引擎
NVelocity引擎输出HTML流***(一般处理程序中) VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetPropert ...
- SVN CornerStone的使用
http://www.henishuo.com/mac-cornerstone-svn-use/
- 图片拉伸(有保护区域) resizableImageWithCapInsets
在仿写QQ会话的时候背景蓝色图片是拉伸而来,但是有些地方是受保护的不能拉伸 所以定义了下面的工具类中的一个方法,专门拉伸图片 UIImageResizingModeStretch:拉伸模式,通过拉伸U ...