ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
def jumps(a):
j = cur = far = 0
for i in range(len(a) - 1):
far = max(far, i + a[i])
if i == cur:
j += 1
cur = far
return j
print(jumps([2, 3, 1, 1, 4]), jumps([2, 3, 0, 1, 4]))- A. 2 3
- B. 1 2
- C. 2 2
- D. 3 2
Correct answer: C. 2 2
Greedy BFS-style jumping adds a jump each time the current reach is used up, then extends the reach to the farthest index seen. Both arrays need exactly 2 jumps.
More Python Programming questions
- What is printed? python def profit(p): lo, best = float('inf'), 0 for x in p: lo = min(lo, x) best = max(best, x - lo) return best…
- What is the time complexity of building a binary heap from n unordered items with heapq.heapify?
- Which statement about bisect.insort(a, x) on a plain Python list a is true?
- What is printed? python import itertools print(list(itertools.accumulate([3, 1, 4], initial=10)))
- What is printed? python from itertools import groupby s = 'aabbaa' print([(k, len(list(g))) for k, g in groupby(s)])
- What is printed? python print(sorted(['b', 'A', 'a', 'B'], key=str.lower))