-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[fix/docs]: improve the Jump Game algorithm #2514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
037b28b
[fix/docs]: improve the Jump Game algorithm
Panquesito7 4a3e7bb
fix: pass `index` by reference and `const` it
Panquesito7 9bbb2fd
fix: use previous algorithm
Panquesito7 44e4c47
chore: apply suggestions from code review
Panquesito7 7ef56d9
Merge branch 'master' into improve_jumpgame
Panquesito7 9e91c18
Merge branch 'master' into improve_jumpgame
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* @file | ||
* @brief [Jumping Game](https://leetcode.com/problems/jump-game/) | ||
* algorithm implementation | ||
* @details | ||
* | ||
* Given an array of non-negative integers, you are initially positioned at the | ||
* first index of the array. Each element in the array represents your maximum | ||
* jump length at that position. Determine if you are able to reach the last | ||
* index. This solution takes in input as a vector and output as a boolean to | ||
* check if you can reach the last position. We name the indices good and bad | ||
* based on whether we can reach the destination if we start at that position. | ||
* We initialize the last index as lastPos. | ||
* Here, we start from the end of the array and check if we can ever reach the | ||
* first index. We check if the sum of the index and the maximum jump count | ||
* given is greater than or equal to the lastPos. If yes, then that is the last | ||
* position you can reach starting from the back. After the end of the loop, if | ||
* we reach the lastPos as 0, then the destination can be reached from the start | ||
* position. | ||
* | ||
* @author [Rakshaa Viswanathan](https://github.com/rakshaa2000) | ||
* @author [David Leal](https://github.com/Panquesito7) | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <iostream> /// for std::cout | ||
#include <vector> /// for std::vector | ||
|
||
/** | ||
* @namespace | ||
* @brief Greedy Algorithms | ||
*/ | ||
namespace greedy_algorithms { | ||
/** | ||
* @brief Checks whether the given element (default is `1`) can jump to the last | ||
* index. | ||
* @param nums array of numbers containing the maximum jump (in steps) from that | ||
* index | ||
* @returns true if the index can be reached | ||
* @returns false if the index can NOT be reached | ||
*/ | ||
template <typename T> | ||
bool can_jump(const std::vector<T> &nums) { | ||
size_t lastPos = nums.size() - 1; | ||
Panquesito7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (int i = nums.size() - 1; i >= 0; i--) { | ||
Panquesito7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Panquesito7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (i + nums[i] >= lastPos) { | ||
lastPos = i; | ||
} | ||
} | ||
return lastPos == 0; | ||
} | ||
} // namespace greedy_algorithms | ||
|
||
/** | ||
* @brief Function to test the above algorithm | ||
* @returns void | ||
*/ | ||
static void test() { | ||
// 1st test | ||
std::vector<int> nums = { 4, 3, 1, 0, 5 }; | ||
assert(greedy_algorithms::can_jump(nums) == true); | ||
|
||
// 2nd test | ||
nums = { 3, 2, 1, 0, 4 }; | ||
assert(greedy_algorithms::can_jump(nums) == false); | ||
|
||
// 3rd test | ||
nums = { 5, 9, 4, 7, 15, 3 }; | ||
assert(greedy_algorithms::can_jump(nums) == true); | ||
|
||
// 4th test | ||
nums = { 1, 0, 5, 8, 12 }; | ||
assert(greedy_algorithms::can_jump(nums) == false); | ||
|
||
// 5th test | ||
nums = {2, 1, 4, 7}; | ||
assert(greedy_algorithms::can_jump(nums) == true); | ||
Panquesito7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
std::cout << "All tests have successfully passed!\n"; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self-test implementations | ||
return 0; | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.