Gridland Metro

  • + 0 comments
    def gridlandMetro(n: Int, m: Int, k: Int, track: Array[Array[Int]]): Long = {
        var ans = 1L * n*m
        track.groupBy(_(0)).values.foreach{ x =>
            x.sortInPlaceBy{_(1)}
            var (l,r) = (x.head(1), x.head(2)) 
            x.tail.foreach { t => (t(1),t(2)) match {
                  case (a,b) if a > r =>
                    ans -= (r - l + 1)
                    l = a
                    r = b
                  case (_,b) => r = r.max(b)
                }
            }
            ans -= (r - l + 1)
        }
      ans
    }