-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yodle-traingle puzzle.java
63 lines (49 loc) · 1.38 KB
/
Yodle-traingle puzzle.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
package Yodle;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
class Yodle{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("triangle.txt"));
String line;
String splits[];
//List Stores each Row
ArrayList<ArrayList<Integer> > rows= new ArrayList<ArrayList<Integer> >();
while ((line = br.readLine()) != null) {
splits=line.split("\\s+");
//Stores elements in a row
ArrayList<Integer> row=new ArrayList<Integer>();
for(int i=0;i<splits.length;i++)
{
row.add(Integer.parseInt(splits[i]));
}
rows.add(row);
}
br.close();
int totalRows=rows.size();
int i=totalRows-1;
while(i>0)
{
ArrayList<Integer> row=getMax(rows.get(i),rows.get(i-1));
rows.remove(i--);
rows.set(i,row);
}
System.out.println("Max Sum is "+rows.get(0).get(0));
}
/*Bottom Up approach calculates the Triangular sum a[i]+b[i],a[i]+b[i+1] and chooses the max
where a is a top row array and b is bottom row array
*/
public static ArrayList<Integer> getMax(ArrayList<Integer> bottom,ArrayList<Integer> top)
{
ArrayList<Integer> maxrow=new ArrayList<Integer>();
for(int i=0;i<top.size();i++)
{
int max=Math.max(top.get(i)+bottom.get(i), top.get(i)+bottom.get(i+1));
maxrow.add(max);
}
return maxrow;
}
}