-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
1272 Remove Interval.swift
46 lines (38 loc) 路 1.29 KB
/
1272 Remove Interval.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// 1272 Remove Interval.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 15/11/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/remove-interval/
class Solution {
/// Removes the intersections between any interval in `intervals` and the
/// interval `toBeRemoved`.
///
/// - Parameters:
/// - intervals: A sorted list of disjoint intervals.
/// - toBeRemoved: The interval to be removed.
/// - Returns: A sorted list of intervals after removals.
///
/// - Complexity:
/// - time: O(n), where n is the number of intervals.
/// - space: O(n), where n is the number of intervals.
func removeInterval(_ intervals: [[Int]], _ toBeRemoved: [Int]) -> [[Int]] {
var ans = [[Int]]()
for interval in intervals {
guard interval[1] > toBeRemoved[0], interval[0] < toBeRemoved[1] else {
ans.append(interval)
continue
}
if interval[0] < toBeRemoved[0] {
ans.append([interval[0], toBeRemoved[0]])
}
if interval[1] > toBeRemoved[1] {
ans.append([toBeRemoved[1], interval[1]])
}
}
return ans
}
}