Created
April 17, 2019 14:24
-
-
Save gantu/3b06c8e150f4e48a66e70458203ae038 to your computer and use it in GitHub Desktop.
The Inquiring Manager -- Solution with immutable state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import scala.annotation.tailrec | |
object Solution{ | |
def main(args: Array[String]) = { | |
val controls = scala.io.StdIn.readInt | |
mainLoop(OrderSate(Map.empty), controls) | |
} | |
@tailrec | |
def mainLoop(orderState: OrderSate, controls: Int) { | |
if(controls > 0){ | |
val line = scala.io.StdIn.readLine.split(" ") | |
line(0) match { | |
case "1" => | |
val newOrders = { | |
orderState.data.get(line(2).toLong) match { | |
case Some(a) => | |
if (a < line(1).toLong) orderState.data + (line(2).toLong -> line(1).toLong) | |
else orderState.data | |
case None => | |
orderState.data + (line(2).toLong -> line(1).toLong) | |
} | |
} | |
val newOrderState = orderState.copy(data = newOrders) | |
mainLoop(newOrderState, controls - 1) | |
case "2" => | |
val t = line(1).toLong | |
printData(orderState, t) | |
mainLoop(orderState, controls - 1) | |
} | |
} | |
} | |
def printData(state: OrderSate, t: Long) = { | |
val orderTimes = state.data.keySet.filter(k => k > t - 60 & k <= t) | |
orderTimes.nonEmpty match { | |
case true => println(orderTimes.map(i => state.data(i)).max) | |
case false => println(-1) | |
} | |
} | |
} | |
case class OrderSate ( | |
data: Map[Long, Long] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment