Skip to content

Commit

Permalink
[Sort] Update solution for the merge intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Feb 9, 2024
1 parent 54513f6 commit d4aec0f
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions Sort/MergeIntervals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,21 @@
*/

class MergeIntervals {
func merge(intervals: [Interval]) -> [Interval] {
var result = [Interval]()

let intervals = intervals.sorted {
if $0.start != $1.start {
return $0.start < $1.start
} else {
return $0.end < $1.end
}
}

for interval in intervals {
guard let last = result.last else {
result.append(interval)
continue
}
func merge(_ intervals: [[Int]]) -> [[Int]] {
let intervals = intervals.sorted { return $0[0] < $1[0] }
var res = [intervals[0]]

for interval in intervals[1..<intervals.count] {

if last.end < interval.start {
result.append(interval)
let lastEnd = res[res.count - 1][1]

if lastEnd < interval[0] {
res.append(interval)
} else {
last.end = max(last.end, interval.end)
res[res.count - 1][1] = max(res[res.count - 1][1], interval[1])
}
}
return result

return res
}
}

0 comments on commit d4aec0f

Please sign in to comment.