A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d6/dba/jump__game_8cpp.html below:

TheAlgorithms/C++: greedy_algorithms/jump_game.cpp File Reference

Loading...

Searching...

No Matches

Jumping Game algorithm implementation More...

#include <cassert>
#include <iostream>
#include <vector>

Go to the source code of this file.

bool  greedy_algorithms::can_jump (const std::vector< int > &nums)   Checks whether the given element (default is 1) can jump to the last index.
static void  test ()   Function to test the above algorithm.
int  main ()   Main function.

Jumping Game algorithm implementation

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.

Definition in file jump_game.cpp.

◆ main()

Main function.

Returns
0 on exit

Definition at line 71 of file jump_game.cpp.

71 {

73 return 0;

74}

static void test()

Function to test the above algorithm.

◆ test()

Function to test the above algorithm.

Returns
void

Definition at line 57 of file jump_game.cpp.

57 {

63

64 std::cout << "All tests have successfully passed!\n";

65}

bool can_jump(const std::vector< int > &nums)

Checks whether the given element (default is 1) can jump to the last index.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4