Stock Maximize problem solution in Scala