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]]))- A. 2 2
- B. 1 2
- C. 1 1
- 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
- What is printed? python def longest(s): st = [-1] best = 0 for i, ch in enumerate(s): if ch == '(': st.append(i) else: st.pop() if not st…
- What is printed? python import heapq from collections import Counter words = 'a b c a b a d d d d'.split() c = Counter(words)…
- What is printed? python from functools import cache @cache def cat(n): if n <= 1: return 1 return sum(cat(i) cat(n - 1 - i) for i in…
- What is printed? python from functools import lrucache grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]] @lrucache(maxsize=None) def best(r, c): if…
- What is printed? python def sigma(n): return sum(d for d in range(1, n + 1) if n % d == 0) perfect = [n for n in range(2, 30) if sigma(n)…
- What is printed? python def phi(n): res, p = n, 2 while p p <= n: if n % p == 0: while n % p == 0: n //= p res -= res // p p += 1 if n >…