P60. 区间合并


打卡 Scala

  case class Interval(start: Int, end: Int)

  // Time: O(n*log(n)), Space: O(1)
  def merge(intervals: collection.Seq[Interval]): Seq[Interval] = {
    val sorted = intervals.sortWith(_.start < _.start)
    sorted.view.tail.foldLeft(Vector(sorted.head)) { (vecs, cur) =>
      val init :+ last = vecs
      if (last.end < cur.start) vecs :+ cur
      else init :+ last.copy(end = math.max(last.end, cur.end))

Leetcode 56 changed the method input to int[][], Is it possible to update the input in the future? thank you!


Of course. The implementation of the new method signature is now added to the algocasts-interview-questions repo. The algorithm is exactly the same, so there is no need to update the video. You will get it once you see the implementation of the new one.

May I know where is the repository? thank you!



It is a private repo. If you are an algocasts member, send me your github id or github profile page link, I will add you to the repo.

By the way, I also add the new implementation to the episode page: https://algocasts.io/episodes/kVG9AYGg