-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
436 Find Right Interval.swift
58 lines (50 loc) 路 1.59 KB
/
436 Find Right 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
47
48
49
50
51
52
53
54
55
56
57
58
//
// 436 Find Right Interval.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 27/08/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/find-right-interval/
class Solution {
/// Checks if there exists an interval j whose start point is bigger than or equal to
/// the end point of the interval i.
///
/// - Parameter intervals: The intervals.
/// - Returns: An array of intervals indexes.
///
/// - Complexity:
/// - time: O(n log(n)), where n is the number of intervals.
/// - space: O(n), where n is the number of intervals.
func findRightInterval(_ intervals: [[Int]]) -> [Int] {
guard !intervals.isEmpty else { return [] }
guard intervals.count > 1 else { return [-1] }
var dict = [Int: Int]()
var sp = [Int]()
for (i, el) in intervals.enumerated() {
dict[el[0]] = i
sp.append(el[0])
}
sp.sort()
var ans = [Int]()
for interval in intervals {
var start = 0
var end = intervals.count - 1
var min = -1
var found = false
while start <= end {
let mid = start + (end - start) / 2
if sp[mid] >= interval[1] {
min = sp[mid]
end = mid - 1
found = true
} else {
start = mid + 1
}
}
ans.append(found ? (dict[min] ?? -1) : -1)
}
return ans
}
}