Skip to content

Commit 63725ee

Browse files
committed
feat: Solution for August Challeng 27 - Find Right Interval
1 parent bcaf683 commit 63725ee

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*:
2+
# Find Right Interval
3+
https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3438/
4+
5+
---
6+
7+
### Problem Statement:
8+
9+
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
10+
11+
For any interval i, you need to store the minimum interval j's index, which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.
12+
13+
14+
### Example 1:
15+
16+
```
17+
Input: [ [1,2] ]
18+
Output: [-1]
19+
20+
Explanation: There is only one interval in the collection, so it outputs -1.
21+
22+
```
23+
24+
### Example 2:
25+
26+
```
27+
Input: [ [3,4], [2,3], [1,2] ]
28+
Output: [-1, 0, 1]
29+
30+
Explanation: There is no satisfied "right" interval for [3,4].
31+
For [2,3], the interval [3,4] has minimum-"right" start point;
32+
For [1,2], the interval [2,3] has minimum-"right" start point.
33+
34+
```
35+
36+
### Example 3:
37+
38+
```
39+
Input: [ [1,4], [2,3], [3,4] ]
40+
41+
Output: [-1, 2, -1]
42+
43+
Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
44+
For [2,3], the interval [3,4] has minimum-"right" start point.
45+
46+
```
47+
48+
### Notes:
49+
+ You may assume the interval's end point is always bigger than its start point.
50+
+ You may assume none of these intervals have the same start point.
51+
52+
*/
53+
54+
55+
import UIKit
56+
57+
class Solution {
58+
// 17 / 17 test cases passed.
59+
// Status: Accepted
60+
// Runtime: 3736 ms
61+
// Memory Usage: 23.2 MB
62+
63+
func findRightInterval1(_ intervals: [[Int]]) -> [Int] {
64+
65+
guard intervals.count > 1 else {
66+
return [-1]
67+
}
68+
69+
let idx = intervals.enumerated().reduce(into: [Int: Int](), { $0[$1.1[0]] = $1.0 })
70+
71+
// Sort the interval by start point
72+
let intervals = intervals.sorted(by: { $0[0] < $1[0] })
73+
74+
var result = [Int](repeating: -1, count: intervals.count)
75+
76+
for i in 0..<(intervals.count - 1) {
77+
for j in (i + 1)..<intervals.count {
78+
if intervals[j][0] >= intervals[i][1] {
79+
result[idx[intervals[i][0]]!] = idx[intervals[j][0]]!
80+
break
81+
}
82+
}
83+
}
84+
85+
return result
86+
}
87+
88+
// 17 / 17 test cases passed.
89+
// Status: Accepted
90+
// Runtime: 384 ms
91+
// Memory Usage: 23.3 MB
92+
93+
// Leetcode Solution using Binary Search
94+
func findRightInterval(_ intervals: [[Int]]) -> [Int] {
95+
// 1. Create Output Array
96+
var result = Array(repeating: -1, count: intervals.count)
97+
98+
// 2. Sort based on start time
99+
var intervals = intervals.enumerated().map { (idx, element) in
100+
return (element, idx)
101+
}
102+
intervals.sort { $0.0[0] < $1.0[0] }
103+
104+
// 3. Perform Binary Search for Each interval
105+
for idx in 0..<intervals.count {
106+
let intervalIdx = intervals[idx].1
107+
let interval = intervals[idx].0
108+
let target = interval[1]
109+
110+
var start = idx+1
111+
var end = intervals.count - 1
112+
while start <= end {
113+
let mid = start + (end - start) / 2
114+
if intervals[mid].0[0] >= target {
115+
if mid == idx + 1 || intervals[mid-1].0[0] < target {
116+
result[intervalIdx] = intervals[mid].1
117+
break
118+
} else {
119+
end = mid - 1
120+
}
121+
} else {
122+
start = mid + 1
123+
}
124+
}
125+
}
126+
127+
return result
128+
}
129+
130+
}
131+
132+
let sol = Solution()
133+
sol.findRightInterval([[1,2]]) // [-1]
134+
//sol.findRightInterval([[3,4], [2,3], [1,2]]) // [-1,0,1]
135+
//sol.findRightInterval([[1,4], [2,3], [3,4]]) // [-1,2,-1]
136+
sol.findRightInterval([[4,5], [2,3], [1,2]]) // [-1,0,1]
137+
138+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)