ProgrammingHardCodeforces-style · placement coding round

What is printed?

What is printed?
def min_remove(iv):
    iv = sorted(iv, key=lambda x: x[1])
    keep = 0
    end = float('-inf')
    for s, e in iv:
        if s >= end:
            keep += 1
            end = e
    return len(iv) - keep
print(min_remove([[1, 2], [2, 3], [3, 4], [1, 3]]), min_remove([[1, 2], [1, 2], [1, 2]]))
  1. A. 2 2
  2. B. 1 2
  3. C. 1 1
  4. D. 0 2

Correct answer: B. 1 2

Sorting by end time and keeping every interval that starts after the last kept end keeps the most intervals. The first case drops one overlapping interval, and three identical intervals keep only one, so two are removed.

More Python Programming questions

Exams that ask this topic