-
Notifications
You must be signed in to change notification settings - Fork 43
/
merge-intervals.py
253 lines (214 loc) · 7.74 KB
/
merge-intervals.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""
56. Merge Intervals
Medium
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
Constraints:
1 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 104
"""
# V0
# IDEA : interval op, LC 57
class Solution(object):
def merge(self, intervals):
# edge case
if not intervals or len(intervals) == 1:
return intervals
"""
NOTE : we sort on x[0] (1st element)
"""
intervals.sort(key = lambda x : x[0])
res = []
for i, j in enumerate(intervals):
if res and res[-1][1] >= intervals[i][0]:
res[-1][1] = max(intervals[i][1], res[-1][1])
else:
res.append([intervals[i][0], intervals[i][1]])
return res
# V0'
# IDEA : interval op + last
class Solution(object):
def merge(self, intervals):
# edge case
if not intervals:
return []
intervals.sort(key = lambda x: (x[0], x[1]))
#print ("intervals = " + str(intervals))
res = []
last = []
for k, v in enumerate(intervals):
if not last:
last = v
elif last and last[1] < v[0]:
res.append(last)
last = v
elif last and last[1] >= v[0]:
last[1] = max(last[1], v[1])
# print ("res = " + str(res))
# print ("last = " + str(last))
if last:
res.append(last)
return res
# V0''
# IDEA : interval op, LC 57
class Solution(object):
def merge(self, intervals):
# edge case
if not intervals:
return
intervals.sort(key=lambda x : x[0])
res = []
for i in range(len(intervals)):
if not res or res[-1][1] < intervals[i][0]:
res.append(intervals[i])
else:
res[-1][1] = max(intervals[i][1], res[-1][1])
return res
# V0'''
# IDEA : interval op
# https://github.com/labuladong/fucking-algorithm/blob/master/%E7%AE%97%E6%B3%95%E6%80%9D%E7%BB%B4%E7%B3%BB%E5%88%97/%E5%8C%BA%E9%97%B4%E8%B0%83%E5%BA%A6%E9%97%AE%E9%A2%98%E4%B9%8B%E5%8C%BA%E9%97%B4%E5%90%88%E5%B9%B6.md
class Solution:
def merge(self, intervals):
intervals = sorted(intervals, key=lambda x: x[0])
result = []
for interval in intervals:
if len(result) == 0 or result[-1][1] < interval[0]:
result.append(interval)
else:
result[-1][1] = max(result[-1][1], interval[1])
return result
# V1
# https://www.jiuzhang.com/solution/merge-intervals/#tag-highlight-lang-python
"""
Definition of Interval.
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
def merge(self, intervals):
intervals = sorted(intervals, key=lambda x: x[0])
result = []
for interval in intervals:
if len(result) == 0 or result[-1][1] < interval[0]:
result.append(interval)
else:
result[-1][1] = max(result[-1][1], interval[1])
return result
# V1''
# https://leetcode.com/problems/merge-intervals/solution/
# IDEA : Approach 1: Connected Components
class Solution:
def overlap(self, a, b):
return a[0] <= b[1] and b[0] <= a[1]
# generate graph where there is an undirected edge between intervals u
# and v iff u and v overlap.
def buildGraph(self, intervals):
graph = collections.defaultdict(list)
for i, interval_i in enumerate(intervals):
for j in range(i+1, len(intervals)):
if self.overlap(interval_i, intervals[j]):
graph[tuple(interval_i)].append(intervals[j])
graph[tuple(intervals[j])].append(interval_i)
return graph
# merges all of the nodes in this connected component into one interval.
def mergeNodes(self, nodes):
min_start = min(node[0] for node in nodes)
max_end = max(node[1] for node in nodes)
return [min_start, max_end]
# gets the connected components of the interval overlap graph.
def getComponents(self, graph, intervals):
visited = set()
comp_number = 0
nodes_in_comp = collections.defaultdict(list)
def markComponentDFS(start):
stack = [start]
while stack:
node = tuple(stack.pop())
if node not in visited:
visited.add(node)
nodes_in_comp[comp_number].append(node)
stack.extend(graph[node])
# mark all nodes in the same connected component with the same integer.
for interval in intervals:
if tuple(interval) not in visited:
markComponentDFS(interval)
comp_number += 1
return nodes_in_comp, comp_number
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
graph = self.buildGraph(intervals)
nodes_in_comp, number_of_comps = self.getComponents(graph, intervals)
# all intervals in each connected component must be merged.
return [self.mergeNodes(nodes_in_comp[comp]) for comp in range(number_of_comps)]
# V1'''
# https://leetcode.com/problems/merge-intervals/solution/
# IDEA : SORTING
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key=lambda x: x[0])
merged = []
for interval in intervals:
# if the list of merged intervals is empty or if the current
# interval does not overlap with the previous, simply append it.
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
# otherwise, there is overlap, so we merge the current and previous
# intervals.
merged[-1][1] = max(merged[-1][1], interval[1])
return merged
# V1'
# https://www.cnblogs.com/zuoyuan/p/3782028.html
class Solution:
# @param intervals, a list of Interval
# @return a list of Interval
def merge(self, intervals):
intervals.sort(key = lambda x:x.start)
length=len(intervals)
res=[]
for i in range(length):
if res==[]:
res.append(intervals[i])
else:
size=len(res)
if res[size-1].start<=intervals[i].start<=res[size-1].end:
res[size-1].end=max(intervals[i].end, res[size-1].end)
else:
res.append(intervals[i])
return res
# V1''
# https://www.cnblogs.com/loadofleaf/p/5084209.html
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
intervals.sort(key = lambda x:x.start)
length = len(intervals)
res = []
if length == 0:
return res
res.append(intervals[0])
for i in range(1,length):
size = len(res)
if res[size - 1].start <= intervals[i].start <= res[size - 1].end:
res[size - 1].end = max(intervals[i].end, res[size - 1].end)
else:
res.append(intervals[i])
return res
# V2