Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Created February 6, 2020 06:35
Show Gist options
  • Save saswata-dutta/652929d0ca0cc6a740ce79ec407734f4 to your computer and use it in GitHub Desktop.
Save saswata-dutta/652929d0ca0cc6a740ce79ec407734f4 to your computer and use it in GitHub Desktop.
object Main {
/*
input: data = [
[1487799425, 14, 1],
[1487799425, 4, 0],
[1487799425, 2, 0],
[1487800378, 10, 1],
[1487801478, 18, 0],
[1487801478, 18, 1],
[1487901013, 1, 0],
[1487901211, 7, 1],
[1487901211, 7, 0]
]
output: 1487800378
# since the increase in the number of people

# in the mall is the highest at that point
List of (ts, num of ppl, entry(1) or exit), sorted by ts
*/
def main(args: Array[String]): Unit = {
val input = Vector(
Footfall(1487799425, 14, true),
Footfall(1487799425, 4, false),
Footfall(1487799425, 2, false),
Footfall(1487800378, 10, true),
Footfall(1487801478, 18, false),
Footfall(1487801478, 18, true),
Footfall(1487901013, 1, false),
Footfall(1487901211, 7, true),
Footfall(1487901211, 7, false)
)
val output = getMaxInsideTs(input)
println(output)
}
case class Footfall(ts: Long, count: Int, isEntry: Boolean)
def getMaxInsideTs(footfalls: Seq[Footfall]): Option[Long] = {
if (footfalls.isEmpty) Option.empty[Long]
else {
val groupped = footfalls.groupBy(_.ts)
val footfallPerTs = groupped.view.mapValues(getNetFootfallInTs)
val cumulativeFootfall =
footfallPerTs.toSeq.sortBy(_._1).scanLeft((0L, 0)){ case ((_, acc), (ts, count)) =>
(ts, count + acc)
}
Option(cumulativeFootfall.maxBy(_._2)._1)
}
}
def getNetFootfallInTs(footfalls: Seq[Footfall]): Int = {
footfalls.map(f => if (f.isEntry) f.count else -f.count).sum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment