My Scala Solution


  • 0
    Z

    object Solution {
    def reconstructQueue(people: Array[Array[Int]]): Array[Array[Int]] = {
    var list : List[Tuple2[Int, Int]] = List()
    for(i<- 0 until people.size)
    {
    list = list :+ Tuple2(people(i)(0),people(i)(1))
    }
    var listres : List[Tuple2[Int, Int]] = List()
    for(v<-list.map(.1).distinct.sortWith(>))
    {
    for(ivl <- list.filter(_.1 == v).sortWith(.2<._2))
    {
    if(ivl._2 >= listres.size){
    listres = listres :+ ivl
    }
    else{
    var split = listres.splitAt(ivl._2)
    listres = (split._1 :+ ivl) ++ split._2
    }
    }
    }
    var res = people.clone
    for(i<-0 until people.size)
    {
    res(i)(0) = listres(i)._1
    res(i)(1) = listres(i)._2
    }
    res
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.