Scala Greedy AC solution


  • 0
    K

    Too bad I have to use 2 vars, one to keep the current upper bound value, one to keep the result.
    Any ideas to eliminate the use of vars to make it more FP, I will be sure to give an upvote.

    object Solution {
        def findLongestChain(pairs: Array[Array[Int]]): Int = {
            var upper = Int.MinValue
            var res = 0
            pairs.sortBy(a => a(1)).map(
                pair => pair match {
                    case p if p(0) > upper => 
                        res += 1
                        upper = p(1)
                    case _ => 
                        
                } 
            )
            res
        }
    }
    

Log in to reply
 

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