File tree Expand file tree Collapse file tree 1 file changed +8
-23
lines changed Expand file tree Collapse file tree 1 file changed +8
-23
lines changed Original file line number Diff line number Diff line change 1
1
# Time: O(nlogn)
2
2
# Space: O(1)
3
3
4
- class Interval (object ):
5
- def __init__ (self , s = 0 , e = 0 ):
6
- self .start = s
7
- self .end = e
8
-
9
- def __repr__ (self ):
10
- return "[{}, {}]" .format (self .start , self .end )
11
-
12
-
13
4
class Solution (object ):
14
5
def merge (self , intervals ):
15
6
"""
16
- :type intervals: List[Interval ]
17
- :rtype: List[Interval ]
7
+ :type intervals: List[List[int] ]
8
+ :rtype: List[List[int] ]
18
9
"""
19
- if not intervals :
20
- return intervals
21
-
22
- intervals .sort (key = lambda x : x .start )
23
- iterator = iter (intervals )
24
- result = [next (iterator )]
25
- for current in iterator :
26
- prev = result [- 1 ]
27
- if current .start <= prev .end :
28
- prev .end = max (current .end , prev .end )
10
+ intervals .sort ()
11
+ result = []
12
+ for interval in intervals :
13
+ if not result or interval [0 ] > result [- 1 ][1 ]:
14
+ result .append (interval )
29
15
else :
30
- result .append (current )
31
-
16
+ result [- 1 ][1 ] = max (result [- 1 ][1 ], interval [1 ])
32
17
return result
You can’t perform that action at this time.
0 commit comments