Package net.n1da.dev.euler
Class PE0018
- java.lang.Object
-
- net.n1da.dev.euler.core.Problem
-
- net.n1da.dev.euler.PE0018
-
- All Implemented Interfaces:
Solvable
- Direct Known Subclasses:
PE0067
public class PE0018 extends Problem
Solution for problem 18 "Maximum path sum I" completed on Sat, 16 May 2015, 21:14.- Since:
- 16.05.2015 06:18:58
- Author:
- Nico Danneberg
- See Also:
- Problem @ Project Euler, Solution @ Ni-Da-Net
-
-
Constructor Summary
Constructors Modifier Constructor Description PE0018()
protected
PE0018(int num, java.lang.String tit)
A simple base constructor to set the givenProblem.number
andProblem.title
.
-
Method Summary
Modifier and Type Method Description void
prepare()
Here the triangle data is initialized, e.g. by reading from a file.java.lang.String
solve()
Finds the path with the biggest sum of all elements through thedata
triangle.protected java.lang.Integer[]
sumTriangleRows(java.lang.Integer[] row1, java.lang.Integer[] row2)
Sums the elements of two given triangle rows.-
Methods inherited from class net.n1da.dev.euler.core.Problem
finish, getResult, getRuntime, toString
-
-
-
-
Constructor Detail
-
PE0018
protected PE0018(int num, java.lang.String tit)
A simple base constructor to set the givenProblem.number
andProblem.title
. In this special case it's needed for inheritance toPE0067
.- Parameters:
num
- - the number of this problemtit
- - the title of this problem
-
PE0018
public PE0018()
-
-
Method Detail
-
prepare
public void prepare()
Here the triangle data is initialized, e.g. by reading from a file.- Specified by:
prepare
in interfaceSolvable
- Overrides:
prepare
in classProblem
- See Also:
IO.readTriangle(String)
-
solve
public java.lang.String solve()
Finds the path with the biggest sum of all elements through thedata
triangle. Therefore all rows are added to each one after another.- Returns:
- the "longest" path (maximum sum)
- See Also:
sumTriangleRows(Integer[], Integer[])
-
sumTriangleRows
protected java.lang.Integer[] sumTriangleRows(java.lang.Integer[] row1, java.lang.Integer[] row2)
Sums the elements of two given triangle rows. Since there are two results for an element of the resulting row, it stores the maximum of both sums.- Parameters:
row1
- the upper row of a triangle that is one element smaller than the row belowrow2
- the lower row that follows directly the first given row- Returns:
- a row with the maximum sum of each pair of elements
-
-