1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Given a triangle similar to the above; starting from the top of the triangle, there would be 2 options (adjacent left and adjacent right). When accumulated continuously downwards, find the biggest possible number.
The approach is actually to start from the bottom and fold the numbers to the top. The following would be the progression with a simple triangle like the above
line 1 => 1
line 2 => 1 2
line 3 => 1 2 3
line 4 => 1 2 3 4
line 5 => 1 2 3 4 5
line 1 => 1
line 2 => 1 2
line 3 => 1 2 3
line 4 => 3 5 7 9
Seeing the bottom 2 lines:
line 4 => 1 2 3 4
line 5 => 1 2 3 4 5
- Between
1
&2
=>2
is larger, so fold2
into the number1
from line 4 => result:3
- Between
2
&3
=>3
is larger, so fold3
into the number2
from line 4 => result:5
- Between
3
&4
=>4
is larger, so fold4
into the number3
from line 4 => result:7
- Between
4
&5
=>5
is larger, so fold5
into the number4
from line 4 => result:9
line 1 => 01
line 2 => 01 02
line 3 => 06 09 12
Seeing the bottom 2 lines:
line 3 => 1 2 3
line 4 => 3 5 7 9
- Between
3
&5
=>5
is larger, so fold5
into the number1
from line 3 => result:6
- Between
5
&7
=>7
is larger, so fold7
into the number2
from line 3 => result:9
- Between
7
&9
=>9
is larger, so fold9
into the number3
from line 3 => result:12
line 1 => 01
line 2 => 10 14
Seeing the bottom 2 lines:
line 2 => 01 02
line 3 => 06 09 12
- Between
06
&09
=>09
is larger, so fold09
into the number1
from line 2 => result:10
- Between
09
&12
=>12
is larger, so fold12
into the number2
from line 2 => result:14
line 1 => 15
Seeing the bottom 2 lines:
line 1 => 01
line 2 => 10 14
- Between
10
&14
=>14
is larger, so fold14
into the number1
from line 1 => result:15
- Go to project root
- Run maven:
mvn test
The test provides 3 sample inputs of varying size with a predetermined maximum sum.