patternMinor
Maximum path problem using Scala
Viewed 0 times
problempathmaximumscalausing
Problem
I am very new to Scala. I have done an exercise using Scala to solve the maximum path problem.
Basically, I have a triangle of integers, I want to find the path from the top to bottom which the numbers on the route produces the largest sum. For example:
Should return:
Could someone please help review the code and make some suggestions about:
Here is the code:
``
case _ => {
val maxIndex = List(rowTransformed(index-1), rowTransformed(index)).zip(List(index - 1, index)).maxBy(_._1)._2
(row(maxIndex), maxIndex)
}
}
}
}
traceBack(input, transformed, max._2, currentLevel + 1) ::: List(max._1)
}
def calcualte(input : List[List[Int]]) : (List[Int], Int) = {
val transformed = sumTree(input, Nil, 0)
val path = traceBack(input.reverse, transformed.reverse, -1, 0)
(path, path.sum)
}
}
object MaxPath {
val input : List[List[Int]] = List(
List(5),
List(12, 3),
Basically, I have a triangle of integers, I want to find the path from the top to bottom which the numbers on the route produces the largest sum. For example:
5
12 3
2 4 9
1 9 12 7Should return:
5 -> 12 -> 4 -> 12
Sum = 33Could someone please help review the code and make some suggestions about:
- Better algorithm
- Writing better scala code
Here is the code:
``
import scala._
class MaxPath {
def sumTree(input : List[List[Int]], tempResult : List[List[Int]], currentlevel : Int) : List[List[Int]] = {
if(currentlevel == input.size) return tempResult
val updatedRow : List[Int] = currentlevel match {
case 0 => input(currentlevel)
case _ => {
val lastRow = tempResult(currentlevel - 1)
val currentRow = input(currentlevel)
val newHead = currentRow.head + lastRow.head
val newLast = currentRow.last + lastRow.last
val middleSection = currentRow.drop(1).dropRight(1)
val newMiddle : List[Int] = for {
(value, index) {
val maxIndex = rowTransformed.zipWithIndex.maxBy(_._1)._2
(row(maxIndex), maxIndex)
}
case x => {
val rowSize = row.size
x match {
case 0 => (row.head, 0)
case rowSize` => (row.last, 0)case _ => {
val maxIndex = List(rowTransformed(index-1), rowTransformed(index)).zip(List(index - 1, index)).maxBy(_._1)._2
(row(maxIndex), maxIndex)
}
}
}
}
traceBack(input, transformed, max._2, currentLevel + 1) ::: List(max._1)
}
def calcualte(input : List[List[Int]]) : (List[Int], Int) = {
val transformed = sumTree(input, Nil, 0)
val path = traceBack(input.reverse, transformed.reverse, -1, 0)
(path, path.sum)
}
}
object MaxPath {
val input : List[List[Int]] = List(
List(5),
List(12, 3),
Solution
In terms of algorithm, I think it is best to focus on the answer you are after. In This case you want the sum, but I notice your result returns the path and the sum.
If you want to focus on the sum think about starting from the bottom, and accessing the best path in terms of the bottom two rows and merging the results up.
Here is an example:
merge last 2 lines (lines 3 and 4)
merge the next line (line 2 and the merged result of 3 and 4)
merge the fist line (line 1 and the merged result of the 2, 3, and 4)
some specific observations about your code
here is a link to my solution to the problem:
https://gist.github.com/dholbrook/6244448#file-euler18-scala
If you want to focus on the sum think about starting from the bottom, and accessing the best path in terms of the bottom two rows and merging the results up.
Here is an example:
5
12, 3
2, 4, 9
1, 9, 12, 7merge last 2 lines (lines 3 and 4)
2 + 9, 4 + 12, 9 + 12merge the next line (line 2 and the merged result of 3 and 4)
12 + (4 + 12), 3 + (9 + 12)merge the fist line (line 1 and the merged result of the 2, 3, and 4)
5 + (12 + (4 + 12))some specific observations about your code
- If you are going to access an element by index, use an indexed collection like Vector
- Consider using fold or reduce instead of recursion with an accumulator
- Props for using immutable variables and data structures
here is a link to my solution to the problem:
https://gist.github.com/dholbrook/6244448#file-euler18-scala
Code Snippets
5
12, 3
2, 4, 9
1, 9, 12, 72 + 9, 4 + 12, 9 + 1212 + (4 + 12), 3 + (9 + 12)5 + (12 + (4 + 12))Context
StackExchange Code Review Q#30845, answer score: 2
Revisions (0)
No revisions yet.