Embercode
✕
Greedy AlgorithmsQuick quiz

Jump game

7 questions. Covers Jump game I and II.

Questions 1 to 5 of 7

  1. 1. For the list [2, 3, 1, 1, 4], what is farthest after the loop in canReachEnd reads index 1?
  2. 2. In minJumps on [2, 3, 1, 1, 4], at which index is the second jump counted?
  3. 3. What does this program print? ``cpp #include <algorithm> #include <cstddef> #include <iostream> #include <vector> int minJumps(const std::vector<int>& nums) { const std::size_t last = nums.size() - 1; if (last == 0) { return 0; } int jumps = 0; std::size_t levelEnd = 0; std::size_t farthest = 0; for (std::size_t i = 0; i < last; ++i) { farthest = std::max(farthest, i + static_cast<std::size_t>(nums[i])); if (i == levelEnd) { if (farthest == i) { return -1; } ++jumps; levelEnd = farthest; if (levelEnd >= last) { return jumps; } } } return -1; } int main() { std::cout << minJumps({2, 0, 2, 0, 1}) << '\n'; } ``
  4. 4. What does this program print? ``cpp #include <algorithm> #include <cstddef> #include <iostream> #include <vector> bool canReachEnd(const std::vector<int>& nums) { std::size_t farthest = 0; for (std::size_t i = 0; i < nums.size(); ++i) { if (i > farthest) { return false; } farthest = std::max(farthest, i + static_cast<std::size_t>(nums[i])); if (farthest >= nums.size() - 1) { return true; } } return true; } int main() { std::cout << std::boolalpha << canReachEnd({2, 1, 0, 0, 4}) << '\n'; } ``
  5. 5. True or false, with the reason: counting one jump on every step of the minJumps loop still gives the fewest number of jumps.

5 question(s) left on this page.