Skip to content

Instantly share code, notes, and snippets.

@songpp
Created January 18, 2011 10:28
Show Gist options
  • Save songpp/784267 to your computer and use it in GitHub Desktop.
Save songpp/784267 to your computer and use it in GitHub Desktop.
package algrithsm
import annotation.tailrec
/**
* User: flower
* Date: 11-1-18
* Time: 下午6:22
* Description:
*/
object Numbers {
import java.lang.Math.max
def maxSum(arr: List[Int]): Int = {
var maxNumber = arr(0)
@tailrec
def maxSumOfArray(numbers: List[Int], maxEnd: Int, maxSum: Int): Int = numbers match {
case (x :: xs) =>
maxNumber = max(x,maxNumber)
val end = max(maxEnd + x, 0)
maxSumOfArray(xs, end, max(end, maxSum))
case _ => maxSum
}
val result = maxSumOfArray(arr, 0, 0)
if(result == 0)
maxNumber
else
result
}
def main(args: Array[String]) {
Console println maxSum(-2 :: -1 :: -3 :: -1 :: Nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment