Skip to content

Commit

Permalink
[Sort] Optimize syntax for Merge Intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Mar 12, 2018
1 parent 1ba2666 commit 00eb59d
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions Sort/MergeIntervals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,29 @@

class MergeIntervals {
func merge(intervals: [Interval]) -> [Interval] {
guard intervals.count > 0 else {
return intervals
}

var intervals = intervals.sort() {
var result = [Interval]()

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

var res = [intervals.first!]

for i in 1..<intervals.count {
let (last, current) = (res.last!, intervals[i])
for interval in intervals {
guard let last = result.last else {
result.append(interval)
continue
}

if current.start > last.end {
res.append(current)
if last.end < interval.start {
result.append(interval)
} else {
last.end = max(last.end, current.end)
last.end = max(last.end, interval.end)
}
}

return res
return result
}
}

0 comments on commit 00eb59d

Please sign in to comment.