2012-01-28 12 views

cevap

14

Ne:

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

'xs.tail.scanLeft (xs.head) ((bkz, x) => (bkz + x) .max (x)). Tüm muhtemelen negatif olabilir, eğer max'. : D – lcn

6

Ben tarama çözümüne katlama çözümü tercih - orada olsa kesinlikle ikincisi için zarafet. Yine de,

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