|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief [Jumping Game](https://leetcode.com/problems/jump-game/) |
| 4 | + * algorithm implementation |
| 5 | + * @details |
| 6 | + * |
| 7 | + * Given an array of non-negative integers, you are initially positioned at the |
| 8 | + * first index of the array. Each element in the array represents your maximum |
| 9 | + * jump length at that position. Determine if you are able to reach the last |
| 10 | + * index. This solution takes in input as a vector and output as a boolean to |
| 11 | + * check if you can reach the last position. We name the indices good and bad |
| 12 | + * based on whether we can reach the destination if we start at that position. |
| 13 | + * We initialize the last index as lastPos. |
| 14 | + * Here, we start from the end of the array and check if we can ever reach the |
| 15 | + * first index. We check if the sum of the index and the maximum jump count |
| 16 | + * given is greater than or equal to the lastPos. If yes, then that is the last |
| 17 | + * position you can reach starting from the back. After the end of the loop, if |
| 18 | + * we reach the lastPos as 0, then the destination can be reached from the start |
| 19 | + * position. |
| 20 | + * |
| 21 | + * @author [Rakshaa Viswanathan](https://github.com/rakshaa2000) |
| 22 | + * @author [David Leal](https://github.com/Panquesito7) |
| 23 | + */ |
| 24 | + |
| 25 | +#include <cassert> /// for assert |
| 26 | +#include <iostream> /// for std::cout |
| 27 | +#include <vector> /// for std::vector |
| 28 | + |
| 29 | +/** |
| 30 | + * @namespace |
| 31 | + * @brief Greedy Algorithms |
| 32 | + */ |
| 33 | +namespace greedy_algorithms { |
| 34 | +/** |
| 35 | + * @brief Checks whether the given element (default is `1`) can jump to the last |
| 36 | + * index. |
| 37 | + * @param nums array of numbers containing the maximum jump (in steps) from that |
| 38 | + * index |
| 39 | + * @returns true if the index can be reached |
| 40 | + * @returns false if the index can NOT be reached |
| 41 | + */ |
| 42 | +template <typename T> |
| 43 | +bool can_jump(const std::vector<T> &nums, int index = 1) { |
| 44 | + const int size = nums.size() + 1 - nums[index]; |
| 45 | + if (nums[index] >= size) { // `>=` because the number can be higher than the size of the array. |
| 46 | + return true; |
| 47 | + } |
| 48 | + return false; |
| 49 | +} |
| 50 | +} // namespace greedy_algorithms |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Function to test above algorithm |
| 54 | + * @returns void |
| 55 | + */ |
| 56 | +static void test() { |
| 57 | + // 1st test |
| 58 | + std::vector<int> nums = { 4, 3, 1, 0, 5 }; |
| 59 | + assert(greedy_algorithms::can_jump(nums) == true); |
| 60 | + |
| 61 | + // 2nd test |
| 62 | + nums = { 3, 2, 1, 0, 4 }; |
| 63 | + assert(greedy_algorithms::can_jump(nums) == false); |
| 64 | + |
| 65 | + // 3rd test |
| 66 | + nums = { 5, 9, 4, 7, 15, 3 }; |
| 67 | + assert(greedy_algorithms::can_jump(nums) == true); |
| 68 | + |
| 69 | + // 4th test |
| 70 | + nums = { 4, 2, 8, 9, 6 }; |
| 71 | + assert(greedy_algorithms::can_jump(nums) == false); |
| 72 | + |
| 73 | + // 5th test |
| 74 | + nums = {7, 4, 8, 13, 2, 11}; |
| 75 | + assert(greedy_algorithms::can_jump(nums) == true); |
| 76 | + |
| 77 | + std::cout << "All tests have successfully passed!\n"; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * @brief Main function |
| 82 | + * @returns 0 on exit |
| 83 | + */ |
| 84 | +int main() { |
| 85 | + test(); // run self-test implementations |
| 86 | + return 0; |
| 87 | +} |
0 commit comments