patternjavaMinor
Project Euler problems 18/67: maximum path sum
Viewed 0 times
pathmaximumprojecteulersumproblems
Problem
I recently solved problems 18/67 in Project Euler. My code is long and I think it could be more effective. I solved the problem with dynamic programming and am new to it, so I want to improve my dynamic programming. I think my running time is acceptable: 0.214 seconds.
```
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class EighteenNSixtyseven {
@SuppressWarnings("rawtypes")
public ArrayList readFile() {
ArrayList nodeList = new ArrayList();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("Triangle.txt"));
String tmp = reader.readLine();
while (tmp != null) {
String[] nums = tmp.split("\\s+");
ArrayList nmb = new ArrayList();
for (int i = 0; i list = ad.readFile(); // reading the file with numbers in triangle
int size = list.size() - 1;
int calc = 0;
int otherCalc = 0;
int q = 0;
ArrayList temp1 = new ArrayList();
ArrayList temp2 = new ArrayList();
while (size >= 1) {
temp1 = list.get(size);
temp2 = list.get(size - 1);
q = 0;
if (temp2.size() != 1) {
while (q otherCalc) { // in the lower line
temp2.set(k, calc); // with the ones in the line
} else { // over to the right and left
temp2.set(k, otherCalc);
}
q++;
}
size--;
}
} else {
calc = temp1.get(0) + temp2.get(0); // had to add this or the
otherCalc = temp1.get(1) + temp2.get(0); // array is outOufBounds
if (calc > otherCalc) {
temp2.set(0, calc);
size--;
} else {
temp2.set(0, otherCalc);
size--;
}
}
}
System.o
```
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class EighteenNSixtyseven {
@SuppressWarnings("rawtypes")
public ArrayList readFile() {
ArrayList nodeList = new ArrayList();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("Triangle.txt"));
String tmp = reader.readLine();
while (tmp != null) {
String[] nums = tmp.split("\\s+");
ArrayList nmb = new ArrayList();
for (int i = 0; i list = ad.readFile(); // reading the file with numbers in triangle
int size = list.size() - 1;
int calc = 0;
int otherCalc = 0;
int q = 0;
ArrayList temp1 = new ArrayList();
ArrayList temp2 = new ArrayList();
while (size >= 1) {
temp1 = list.get(size);
temp2 = list.get(size - 1);
q = 0;
if (temp2.size() != 1) {
while (q otherCalc) { // in the lower line
temp2.set(k, calc); // with the ones in the line
} else { // over to the right and left
temp2.set(k, otherCalc);
}
q++;
}
size--;
}
} else {
calc = temp1.get(0) + temp2.get(0); // had to add this or the
otherCalc = temp1.get(1) + temp2.get(0); // array is outOufBounds
if (calc > otherCalc) {
temp2.set(0, calc);
size--;
} else {
temp2.set(0, otherCalc);
size--;
}
}
}
System.o
Solution
Even before starting the review I have to disappoint you. The problem has nothing to do with the dynamic programming. Now let's go.
-
Naming: Avoid meaningless names, such as
-
Responsibilities: You correctly separated IO in a method of its own. The actual calculations also deserve to be separated.
-
Algorithm: A triple nesting loop is an immediate red flag. In fact, you don't need to iterate over
The core loop should look like
That said, let me reiterate a very important rule of no raw loops: for every loop you write, think of it as a standalone method and figure out a meaningful name for it. If you can't than you effectively don't know what it is doing!
-
Naming: Avoid meaningless names, such as
q, temp1, temp2, calc and otherCalc.-
Responsibilities: You correctly separated IO in a method of its own. The actual calculations also deserve to be separated.
-
Algorithm: A triple nesting loop is an immediate red flag. In fact, you don't need to iterate over
q (which is the index into the bottom row, right? - it took me a while to figure it out) at all: the kth element of the upper row can be only influenced by kth and k+1th of the bottom one. Taking it into consideration, you'd eliminate one level of nesting as well as a really ugly special case of temp2.size() == 1. The core loop should look like
for (k = 0; k < upperRow.size(); k++)
upperRow[k] += max(bottomRow[k], bottomRow[k+1]);That said, let me reiterate a very important rule of no raw loops: for every loop you write, think of it as a standalone method and figure out a meaningful name for it. If you can't than you effectively don't know what it is doing!
Code Snippets
for (k = 0; k < upperRow.size(); k++)
upperRow[k] += max(bottomRow[k], bottomRow[k+1]);Context
StackExchange Code Review Q#51021, answer score: 7
Revisions (0)
No revisions yet.