scala SortedMap
scala\nobject Solution {\n  import collection.immutable.SortedMap\n  def fallingSquares(positions: Array[Array[Int]]): List[Int] =\n    positions.map(_.toList)\
```scala\nobject Solution {\n  import collection.immutable.SortedMap\n  def fallingSquares(positions: Array[Array[Int]]): List[Int] =\n    positions.map(_.toList)\n      .foldLeft((List(-1),SortedMap.empty[Int,(Int,Int)])){case ((l,aMap),p) =>\n        lazy val y2 = aMap.maxBefore(p.head).filter{case (k,(len,_)) => k+len > p.head}.toList ++\n          aMap.iteratorFrom(p.head).takeWhile(_._1 < p.sum)\n        lazy val newHeight = y2.map(_._2._2).maxOption.getOrElse(0) + p.last\n        lazy val newSegments = List(\n          y2.headOption.map{case (k,(_,h)) => (k, (p.head - k,h))}\n            .filterNot(x=>x._1<=0 || x._2._1<=0),\n          Some(p.head,(p.last,newHeight)),\n          y2.lastOption.map{case (k,(v,h)) => (p.sum, ((k+v)-p.sum,h))}\n            .filterNot(x=>x._1<=0 || x._2._1<=0)\n        ).flatten\n        lazy val newMap = aMap -- y2.map(_._1) ++ newSegments\n        ((l.head max newHeight) +: l, newMap )\n      }._1.reverse.tail\n}\n```
['Ordered Map', 'Scala']