ProgrammingVery hardCAT · MBA placement
You must choose the maximum number of non-overlapping activities, each with a start and a finish time. Which greedy rule is correct?
- A. Sort by start time and take each activity that starts after the last chosen start
- B. Sort by duration, shortest first, and take every compatible activity
- C. Sort by finish time and take each activity whose start is at least the last chosen finish
- D. Pick the activity that overlaps the fewest others, then remove overlaps and repeat
Correct answer: C. Sort by finish time and take each activity whose start is at least the last chosen finish
Earliest finish is the correct greedy choice because it leaves the most room for later activities. Sorting by start or duration can pick one long activity that blocks several short ones.
More Python Programming questions
- What is printed? python a = [4, 1, 7, 3, 2] pre = [0] for x in a: pre.append(pre[-1] + x) def rng(l, r): return pre[r] - pre[l - 1]…
- What is printed? python def mostwater(h): i, j, best = 0, len(h) - 1, 0 while i < j: best = max(best, min(h[i], h[j]) (j - i)) if h[i] <…
- What is printed? python def longest(s): last = {} l = best = 0 for r, ch in enumerate(s): if ch in last and last[ch] >= l: l = last[ch] +…
- What is printed? python import bisect a = sorted([5, 1, 9, 5, 3, 7, 5]) lo = bisect.bisectleft(a, 3) hi = bisect.bisectright(a, 7)…
- A Python program must answer 10^6 membership queries (x in container) against a container with 10^6 items. Which statement is correct?
- What is printed? python def ways(coins, amt): dp = [0] (amt + 1) dp[0] = 1 for c in coins: for v in range(c, amt + 1): dp[v] += dp[v - c]…