欧拉项目第18题Maximum path sum I

来源:互联网 发布:linux less 向上查找 编辑:程序博客网 时间:2024/06/05 15:54

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3
7 4
4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

从上到下,每次只能取相邻的数,求这些数和最大值。

思路如下,每一行看成一个数组,第一行是75,没啥说的,

到第二行,算出这个位数,线路最大值,放入数组里面,第二个是 75+95=170,64+75=139,

第三行, 170+17 = 187,MAX(170,139) + 47=217, 139 + 82= 221

第四行, 187 +18,  MAX(187217) + 35,   MAX(217221) + 87, 221+10

...


 static Map<Integer,Integer[]> x = new HashMap<Integer, Integer[]>();        static {        x.put(1, new Integer[]{75});         x.put(2, new Integer[]{95,64});        x.put(3, new Integer[]{17,47,82});        x.put(4, new Integer[]{18,35,87,10});        x.put(5, new Integer[]{20,4,82,47,65});        x.put(6, new Integer[]{19,1,23,75,3,34});        x.put(7, new Integer[]{88,2,77,73,7,63,67});        x.put(8, new Integer[]{99,65,4,28,6,16,70,92});        x.put(9, new Integer[]{41,41,26,56,83,40,80,70,33});        x.put(10, new Integer[]{41, 48, 72, 33, 47, 32, 37, 16, 94, 29});        x.put(11, new Integer[]{53,71, 44, 65, 25, 43, 91, 52, 97, 51, 14});        x.put(12, new Integer[]{70,11,33,28,77,73,17,78,39,68,17,57});        x.put(13, new Integer[]{91,71,52,38,17,14,91,43,58,50,27,29,48});        x.put(14, new Integer[]{63,66,4,68,89,53,67,30,73,16,69,87,40,31});        x.put(15, new Integer[]{4,62,98,27,23,9,70,98,73,93,38,53,60,4,23});    }        public static void main(String[] args) {        Integer[] y = new Integer[]{75};        for(int i=2;i<=15;i++) {            Integer[] t_x = x.get(i);            Integer[] on = new Integer[i];            for(int j=0;j<i;j++) {                if(j==0) {                    on[j] = y[0] + t_x[0];                } else if(j == i - 1) {                    on[j] = y[j-1] + t_x[j];                } else {                    on[j] = Math.max(y[j-1], y[j]) + t_x[j];                }            }            y = on;        }        Integer max =0;        for(Integer yy : y) {            if(max < yy) {                max = yy;            }        }        System.out.println(max);    }


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 二建拿到证书原单位不解锁怎么办 凯云软件清单锁定只读了怎么办 苹果笔记本鼠标触摸板没反应怎么办 苹果笔记本键盘和触摸板失灵怎么办 苹果手机输入密码显示已停用怎么办 苹果7p手机刷机黑屏了怎么办 苹果5s来电接听屏幕卡顿怎么办? 手机摔了一下触屏失灵怎么办 小米手机摔了一下触屏失灵怎么办 苹果6sp触摸ic坏了怎么办 苹果4s屏幕摔裂了怎么办 新换的手机内屏颜色太亮怎么办 苹果手机摔了一下屏幕失灵怎么办 苹果手机6s屏幕坏了怎么办 苹果6s屏幕摔坏了怎么办 苹果手机屏幕进油了屏幕变暗怎么办 苹果手机不小心屏幕进油了怎么办 苹果6充电插口螺丝口坏了怎么办 苹果5s手机安装屏幕翘边怎么办 苹果手机摔了一下触摸屏失灵怎么办 苹果手机摔了下触摸屏失灵怎么办 苹果6老是屏幕失灵或者闪屏怎么办 华为荣耀8手触摸屏乱跳失灵怎么办 苹果5s屏幕有半边竖纹怎么办 苹果5s电源键坏了怎么办 苹果6s手机触屏失灵怎么办 不能取电池的手机触屏不灵怎么办? 7p主板触摸ic坏了怎么办 小米六手机摔了一下屏幕失灵怎么办 魅蓝5s触屏失灵怎么办 苹果手机键盘字母顺序乱了怎么办 红米手机进水后屏幕失灵怎么办 苹果7plus主屏按键坏了怎么办 苹果六手机上的红色变成粉色怎么办 苹果手机出现闪屏和手感不灵怎么办 荣耀6plus后开不了机怎么办 苹果手机touch id密码忘了怎么办 苹果7摔出了一个裂缝怎么办 屏幕摔坏了下键盘失灵怎么办 苹果4s手机按健失灵怎么办 金立手机摔了一下黑屏了怎么办