2012-01-28 9 views

risposta

14

Che dire di questo:

numbers.scanLeft(0)((acc, n) => math.max(0, acc + n)).max 
+5

'xs.tail.scanLeft (xs.head) ((acc, x) => (acc + x) .max (x)). Max' se tutti possono eventualmente essere negativo. : D – lcn

6

io preferisco la soluzione pieghevole per la soluzione di scansione - anche se c'è sicuramente eleganza a questi ultimi. In ogni caso,

numbers.foldLeft(0 -> 0) { 
    case ((maxUpToHere, maxSoFar), n) => 
    val maxEndingHere = 0 max maxUpToHere + n 
    maxEndingHere -> (maxEndingHere max maxSoFar) 
}._2