Embercode
✕
Greedy AlgorithmsQuick quiz

Jump game in Python

7 questions. Covers Jump game I and II in Python.

Questions 1 to 5 of 7

  1. 1. In jump game I, what does the variable farthest hold?
  2. 2. What does this code print? ``python def can_reach_end(nums): last = len(nums) - 1 farthest = 0 for i in range(last + 1): if i > farthest: return False farthest = max(farthest, i + nums[i]) if farthest >= last: return True return True print(can_reach_end([1, 0, 2, 0, 0])) print(can_reach_end([3, 0, 0, 1])) ``
  3. 3. What does this code print? ``python def min_jumps_off(nums): last = len(nums) - 1 jumps = 0 cur_end = 0 farthest = 0 for i in range(last + 1): farthest = max(farthest, i + nums[i]) if i == cur_end: jumps += 1 cur_end = farthest return jumps print(min_jumps_off([1, 1, 1, 1])) ``
  4. 4. In jump game II, the sweep runs from index 0 to last - 1, not to last. Why?
  5. 5. True or False: in jump game I, the test if i >= farthest: return False gives the right answer, and i > farthest is only a style choice.

5 question(s) left on this page.