欧拉项目第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
2 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(187,217) + 35, MAX(217,221) + 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); }
- 欧拉项目第18题Maximum path sum I
- Maximum path sum I
- Problem 18:Maximum path sum I
- Maximum path sum I Problem 18
- PE 18 Maximum path sum I
- project euler 18 Maximum path sum I
- No18:Maximum path sum I
- Maximum path sum I [Project Euler.Problem 18]
- projecteuler---->problem=18----Maximum path sum I
- Project Euler:Problem 18 Maximum path sum I
- Problem 18 Maximum path sum I (dp)
- Project Euler Problem 18 Maximum path sum I
- [projecteuler]Maximum path sum I,II
- leetcode 第124题 Binary Tree Maximum Path Sum
- 欧拉项目第13题 Large sum
- 欧拉项目第16题 Power digit sum
- 欧拉项目 第20题 Factorial digit sum
- PAT A 1007. Maximum Subsequence Sum(贪心算法应用path[i] 与 path[i + 1)
- <c:if></c:if>中多个EL表达式
- jquery.chosen 强大的jquery模拟下拉框(select)插件
- 双链表逆向输出
- Nginx 错误处理方法: bind() to 0.0.0.0:80 failed
- iOS中为控件设置颜色渐变和透明度渐变
- 欧拉项目第18题Maximum path sum I
- CarouselViewPager实现ViewPager的轮播效果
- WebP 探寻之路
- js 继承
- Aizu - 0033(深搜)
- 驱动1
- js 文件提交
- Multiple markers at this line @Override的解决方法
- JNI_Android项目中调用.so动态库实现详解(初探)