Skip to content

Commit

Permalink
Add scala solution for 435-Non-Overlapping-Intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKheng committed Aug 2, 2022
1 parent bb83ba4 commit 75e8aa9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions scala/435-Non-Overlapping-Intervals.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
object Solution {
def eraseOverlapIntervals(intervals: Array[Array[Int]]): Int = {
val (start, end) = (0, 1)
val sortedIntervals = intervals.sortBy(_(0))

var numRemovals = 0
var currEnd = sortedIntervals(0)(end)

for (i <- 1 until sortedIntervals.length) {
val currInterval = sortedIntervals(i)

if (currInterval(start) < currEnd) {
numRemovals += 1
currEnd = currEnd.min(currInterval(end))
} else {
currEnd = currInterval(end)
}
}

numRemovals
}
}

0 comments on commit 75e8aa9

Please sign in to comment.