-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0435-non-overlapping-intervals.rb
60 lines (49 loc) · 1.58 KB
/
0435-non-overlapping-intervals.rb
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
# frozen_string_literal: true
# 435. Non-overlapping Intervals
# https://leetcode.com/problems/non-overlapping-intervals
# Medium
=begin
Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Example 1:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2:
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
Example 3:
Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
Constraints:
1 <= intervals.length <= 105
intervals[i].length == 2
-5 * 104 <= starti < endi <= 5 * 104
=end
# @param {Integer[][]} intervals
# @return {Integer}
def erase_overlap_intervals(intervals)
return 0 if intervals.count <= 1
result = 0
end_point = -Float::INFINITY
intervals.sort_by(&:last).each do |interval|
if interval.first >= end_point
end_point = interval.last
else
result += 1
end
end
result
end
# ********************#
# TEST #
# ********************#
require "test/unit"
class Test_erase_overlap_intervals < Test::Unit::TestCase
def test_
assert_equal 1, erase_overlap_intervals([[1, 2], [2, 3], [3, 4], [1, 3]])
assert_equal 2, erase_overlap_intervals([[1, 2], [1, 2], [1, 2]])
assert_equal 0, erase_overlap_intervals([[1, 2], [2, 3]])
end
end