Maximum path sum II
Problem 67

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 27.

That is, 5+9+6+7= 27.

Find the maximum total from top to bottom in triangle.txt (right click and ‘Save Link/Target As…’), a 15K text file containing a triangle with one-hundred rows.

NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem, as there are 299 altogether! If you could check one trillion (1012) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o)

Here is a solution in java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class AcyclicDigraph {
 
	List<List> nodeList = new ArrayList<List>();
 
	public static void main(String[] args) {
		AcyclicDigraph ad = new AcyclicDigraph();
		Scanner s = new Scanner(System.in);
		System.out.println("Enter file path:");
		String filePath = s.nextLine();
		ad.readFile(filePath);
		int index = 0;
		int num1 = 0, num2 = 0, sum = 0;
		boolean first = true;
		for (List a : ad.nodeList) {
			if (first) {
				num1 = (Integer) a.get(index);
				sum = num1;
				System.out.print(num1);
				first = false;
			} else {
				num1 = (Integer) a.get(index);
				num2 = (Integer) a.get(index + 1);
				if (num1 > num2) {
					sum += num1;
					System.out.print("+" + num1);
				} else {
					sum += num2;
					index++;
					System.out.print("+" + num2);
				}
			}
 
		}
		System.out.println("\nThe sum total is = " + sum);
	}
 
	public void readFile(String path) {
		BufferedReader bis = null;
		try {
			bis = new BufferedReader(new FileReader(path));
			String tmp = bis.readLine();
			while (tmp != null) {
				String[] nums = tmp.split("\\s+");
				List<Integer> a = new ArrayList<Integer>();
				for (int i = 0; i < nums.length; i++) {
					a.add(Integer.parseInt(nums[i]));
				}
				nodeList.add(a);
				tmp = bis.readLine();
			}
 
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bis.close();
			} catch (Exception e) {
				e.printStackTrace();
 
			}
		}
 
	}
 
}

The result of running the above program with input file triangle.txt is:

59+73+52+53+87+66+80+76+87+58+78+93+78+42+9+94+42+98+28+81+70+58+39+45+62+57+31+24+90+17+89+69+73+52+39+90+84+52+95+65+94+64+96+22+58+45+56+82+74+52+98+38+91+78+90+70+61+17+65+87+90+24+48+44+64+77+97+99+76+84+67+25+61+90+75+46+96+76+77+56+86+69+90+84+76+55+49+19+45+88+48+99+49+35+87+53+99+64+77+63
The sum total is = 6580

Links: Maximum path sum II – Problem 67

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>