Skip to content

Instantly share code, notes, and snippets.

@Odomontois
Created December 21, 2020 23:10
Show Gist options
  • Save Odomontois/0ad1fd2128525533f4a4b5989e3ab606 to your computer and use it in GitHub Desktop.
Save Odomontois/0ad1fd2128525533f4a4b5989e3ab606 to your computer and use it in GitHub Desktop.
import collection.immutable.TreeSet
object Solution {
case class Item(cur: Int, next: List[Int], id: Int)
implicit val itemOrdering: Ordering[Item] = Ordering.by(i => (i.cur,i.next.headOption, i.id))
def go(s: TreeSet[Item]): LazyList[Vector[Int]] =
Vector(s.head.cur, s.last.cur) #:: (s.head match {
case Item(_, x :: rest, id) => go(s.tail + Item(x, rest, id))
case _ => LazyList.empty
})
def smallestRange(nums: List[List[Int]]): Array[Int] =
go(nums.zipWithIndex.collect{ case (x :: rest, id) => Item(x, rest, id) }.to(TreeSet))
.minBy{ case Vector(a, b) => b - a }.toArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment