Browse Source

adding working solution - from bottom.

master
Charles Reid 8 years ago
parent
commit
c4a772dcdc
  1. 29
      067/Bottom.java
  2. 44
      067/BruteForce.java
  3. 54
      067/Path.java

29
067/Bottom.java

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
import java.util.Arrays;
public class Bottom {
public static void main(String[] args) {
System.out.println(fromBottom());
}
public static int fromBottom() {
int[][] triangle = LoadTriangle.loadTriangle();
int[][] triangleSum = LoadTriangle.loadTriangle();
int dim = triangle.length;
int sum = 0;
// Rows go from second to last to first
for(int r=dim-2; r>=0; r--) {
// Columns go from 0 to r
for(int j=0; j<=r; j++) {
// Find maximum path for this three-element subtree
int left = triangleSum[r+1][j];
int right = triangleSum[r+1][j+1];
if(left > right) {
triangleSum[r][j] = triangleSum[r+1][j] + triangle[r][j];
} else {
triangleSum[r][j] = triangleSum[r+1][j+1] + triangle[r][j];
}
}
}
return triangleSum[0][0];
}
}

44
067/BruteForce.java

@ -1,25 +1,19 @@ @@ -1,25 +1,19 @@
import java.util.Arrays;
import java.util.Scanner;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
public class BruteForce {
public static final String FILE = "small.txt";
public static int max = 0;
public static String maxPath = "";
public static void main(String[] args) throws FileNotFoundException {
public static void main(String[] args) {
System.out.println(bruteForcePath());
System.out.println(maxPath);
}
public static int bruteForcePath() throws FileNotFoundException {
int[][] triangle = loadTriangle();
public static int bruteForcePath() {
int[][] triangle = LoadTriangle.loadTriangle();
int dim = triangle.length;
boolean[] goright = new boolean[dim];
@ -81,37 +75,5 @@ public class BruteForce { @@ -81,37 +75,5 @@ public class BruteForce {
}
return sum;
}
/** Load a triangle from a file. */
public static int[][] loadTriangle() throws FileNotFoundException {
int nlines = countLines();
int[][] triangle = new int[nlines][nlines];
Scanner s = new Scanner(new BufferedReader(new FileReader(FILE)));
int r = 0;
while(s.hasNextLine()) {
String line = s.nextLine();
Scanner ls = new Scanner(line);
int c = 0;
while(ls.hasNextInt()) {
int num = ls.nextInt();
triangle[r][c] = num;
c++;
}
r++;
}
return triangle;
}
/** Count the number of lines in the triangle file. */
public static int countLines() throws FileNotFoundException {
Scanner s = new Scanner(new BufferedReader(new FileReader(FILE)));
int lines = 0;
while(s.hasNextLine()) {
s.nextLine();
lines++;
}
return lines;
}
}

54
067/Path.java

@ -1,13 +1,7 @@ @@ -1,13 +1,7 @@
import java.util.Arrays;
import java.util.Scanner;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
public class Path {
public static final String FILE = "triangle_counterexample1.txt";
/** Find the maximum path down a triangle.
*
* Strategy: add up the marginal sums of each diagonal,
@ -15,14 +9,14 @@ public class Path { @@ -15,14 +9,14 @@ public class Path {
* and use those measures to make decisions about which
* directions to move in.
*/
public static void main(String[] args) throws FileNotFoundException {
public static void main(String[] args) {
System.out.println(navigateMaxPath());
}
/** Navigate the triangle one step at a time, making decisions as we go about which direction to move. */
public static int navigateMaxPath() throws FileNotFoundException {
int[][] triangle = loadTriangle();
public static int navigateMaxPath() {
int[][] triangle = LoadTriangle.loadTriangle();
int dim = triangle.length;
int crawl = 0;
@ -33,8 +27,8 @@ public class Path { @@ -33,8 +27,8 @@ public class Path {
System.out.print(triangle[rr][cc] + " ");
if(rr<dim-1 && cc<dim-1) {
System.out.println();
System.out.println(" left sum = "+subtreeSum(rr+1,cc,triangle) );
System.out.println(" right sum = "+subtreeSum(rr+1,cc+1,triangle) );
System.out.println(" left sum = " +subtriangleSum(rr+1,cc,triangle) );
System.out.println(" right sum = "+subtriangleSum(rr+1,cc+1,triangle) );
}
if(goLeft(rr,cc,triangle)) {
@ -50,14 +44,14 @@ public class Path { @@ -50,14 +44,14 @@ public class Path {
/** Determine whether we should go left at row i, column j of the triangle. */
public static boolean goLeft(int i, int j, int[][] triangle) {
int leftSubtreeSum = subtriangleSum( i+1, j, triangle);
int rightSubtreeSum = subtriangleSum( i+1, j+1, triangle);
int leftSum = subtriangleSum( i+1, j, triangle);
int rightSum = subtriangleSum( i+1, j+1, triangle);
int dim = triangle.length;
// break ties by value, but skip last row
if(leftSubtreeSum==rightSubtreeSum && i+1>dim) {
if(leftSum==rightSum && i+1>dim) {
return (triangle[i+1][j] > triangle[i+1][j+1]);
} else {
return leftSubtreeSum > rightSubtreeSum;
return leftSum > rightSum;
}
}
@ -107,34 +101,4 @@ public class Path { @@ -107,34 +101,4 @@ public class Path {
}
/** Load a triangle from a file. */
public static int[][] loadTriangle() throws FileNotFoundException {
int nlines = countLines();
int[][] triangle = new int[nlines][nlines];
Scanner s = new Scanner(new BufferedReader(new FileReader(FILE)));
int r = 0;
while(s.hasNextLine()) {
String line = s.nextLine();
Scanner ls = new Scanner(line);
int c = 0;
while(ls.hasNextInt()) {
int num = ls.nextInt();
triangle[r][c] = num;
c++;
}
r++;
}
return triangle;
}
/** Count the number of lines in the triangle file. */
public static int countLines() throws FileNotFoundException {
Scanner s = new Scanner(new BufferedReader(new FileReader(FILE)));
int lines = 0;
while(s.hasNextLine()) {
s.nextLine();
lines++;
}
return lines;
}
}
Loading…
Cancel
Save