File tree Expand file tree Collapse file tree 2 files changed +12
-14
lines changed Expand file tree Collapse file tree 2 files changed +12
-14
lines changed Original file line number Diff line number Diff line change 16
16
17
17
class MeetingRooms {
18
18
func canAttendMeetings( intervals: [ Interval ] ) -> Bool {
19
- if intervals. count < 2 {
19
+ guard intervals. count > 1 else {
20
20
return true
21
21
}
22
22
23
23
var intervals = intervals. sort ( { $0. start < $1. start} )
24
24
25
- for i in 0 ... intervals. count - 2 {
25
+ for i in 0 ..< intervals. count - 1 {
26
26
if intervals [ i] . end > intervals [ i + 1 ] . start {
27
27
return false
28
28
}
Original file line number Diff line number Diff line change 16
16
17
17
class MergeIntervals {
18
18
func merge( intervals: [ Interval ] ) -> [ Interval ] {
19
- if intervals. count <= 1 {
19
+ guard intervals. count > 1 else {
20
20
return intervals
21
21
}
22
22
23
- var intervals = intervals. sort ( sortIntervals)
23
+ var intervals = intervals. sort ( ) {
24
+ if $0. start != $1. start {
25
+ return $0. start < $1. start
26
+ } else {
27
+ return $0. end < $1. end
28
+ }
29
+ }
24
30
25
31
var res = [ Interval] ( )
26
32
res. append ( intervals [ 0 ] )
27
33
28
34
for i in 1 ..< intervals. count {
29
- var last = res [ res. count - 1 ]
30
- var current = intervals [ i]
35
+ let last = res [ res. count - 1 ]
36
+ let current = intervals [ i]
31
37
if current. start > last. end {
32
38
res. append ( current)
33
39
} else {
@@ -37,12 +43,4 @@ class MergeIntervals {
37
43
38
44
return res
39
45
}
40
-
41
- private func sortIntervals( p: Interval , q: Interval ) -> Bool {
42
- if p. start != q. start {
43
- return p. start < q. start
44
- } else {
45
- return p. end < q. end
46
- }
47
- }
48
46
}
You can’t perform that action at this time.
0 commit comments