Created
December 21, 2020 23:10
-
-
Save Odomontois/0ad1fd2128525533f4a4b5989e3ab606 to your computer and use it in GitHub Desktop.
This file contains 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
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