Count the number of ways a person can walk, roll or jump
Last Updated :
30 Nov, 2023
A person has to cover a distance, He can either walk, roll, or jump, each of these actions covers one unit distance. These actions are represented by: walk: w, roll: r, jump: j. Each person is eligible for the trophy if they meet the following criteria :
- The Person rolled for less than 2 unit distance in total.
- The Person jumped less than 3 consecutive unit distances.
Given an integer n, return the number of possible ways the person will be traveling n distance and be eligible for the trophy. The answer may be very large, so return it modulo 109 + 7.
Examples:
Input: 3
Output: 19
Explanation: There are in total 19 ways to travel the distance by following the rules. These 19 ways are-
- All Walk- WWW,
- Two Walk- WWR, WRW, RWW, WWJ, WJW, JWW,
- One Walk- WJJ, JWJ, JJW, WJR, WRJ, RWJ, RJW, JWR, JRW,
- Zero Walk- JJR, JRJ, RJJ
Input: 5
Output: 94
Explanation: There are in total 94 ways to travel the distance by following the rules.
Input: 7
Output: 418
Explanation: There are in total 418 ways to travel the distance by following the rules.
Approach: To solve the problem follow the below idea:
- int ‘solver(int n, int roll ,int jump)’: This is a recursive function that calculates the number of valid sequences of rolls, walks and jumps given the current state of steps, total rolls, and consecutive jumps.
- int w = solver(n-1, roll, 0): If the player walks (no rolling or jumping), total roll remains as it is and consecutive jump is reset to 0.
- int r = solver(n-1,roll+1, 0): If the player chooses to roll, total roll is incremented by 1, and consecutive jump is reset to 0.
- int j = solver(n-1, roll, jump+1): If the player chooses to jump, total roll remains the same, and consecutive jump is incremented by 1.
- To handle consecutive jumps, we need to ensure that the jump variable is incremented only in a single function call (only when the person jumps), and resets to zero in all other function calls. This way, we’re specifically tracking the consecutive jumps without introducing additional variations.
- This approach allows us to distinguish between scenarios where jumps are consecutive versus when they are not. It ensures that we’re addressing the specific case of consecutive jumps without introducing complexities related to total jumps across multiple calls. This way, we maintain a clear focus on handling consecutive jumps, which is the intended objective of the code.
- if (jump >= 3 or roll >= 2) return 0; – If there are three or more consecutive jumps (jump >= 3) or two or more consecutive rolls (roll >= 2), then the sequence is not considered valid, so it returns 0.
- if(n == 0) return 1; – If n reaches 0, it means we have successfully completed the sequence, so it returns 1 (indicating one valid sequence).
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int dp[100001][2][3];
int solver( int n, int roll, int jump)
{
if (jump >= 3 or roll >= 2) {
return 0;
}
if (n == 0)
return 1;
if (dp[n][roll][jump] != -1)
return dp[n][roll][jump];
int w = solver(n - 1, roll, 0);
int r = solver(n - 1, roll + 1, 0);
int j = solver(n - 1, roll, jump + 1);
return dp[n][roll][jump] = ((w + r) % mod + j) % mod;
}
int checkDistance( int n)
{
memset (dp, -1, sizeof (dp));
return solver(n, 0, 0);
}
int main()
{
cout << checkDistance(6);
return 0;
}
|
Java
class GFG {
static int mod = ( int )(1e9 + 7 );
static int [][][] dp = new int [ 100001 ][ 2 ][ 3 ];
static int solver( int n, int roll, int jump)
{
if (jump >= 3 || roll >= 2 ) {
return 0 ;
}
if (n == 0 )
return 1 ;
if (dp[n][roll][jump] != - 1 )
return dp[n][roll][jump];
int w = solver(n - 1 , roll, 0 );
int r = solver(n - 1 , roll + 1 , 0 );
int j = solver(n - 1 , roll, jump + 1 );
return dp[n][roll][jump]
= ((w + r) % mod + j) % mod;
}
static int checkDistance( int n)
{
for ( int i = 0 ; i < 100001 ; i++) {
for ( int j = 0 ; j < 2 ; j++) {
for ( int k = 0 ; k < 3 ; k++) {
dp[i][j][k] = - 1 ;
}
}
}
return solver(n, 0 , 0 );
}
public static void main(String[] args)
{
System.out.println(checkDistance( 6 ));
}
}
|
Python3
mod = 10 * * 9 + 7
dp = [[[ - 1 for _ in range ( 3 )] for _ in range ( 2 )] for _ in range ( 100001 )]
def solver(n, roll, jump):
if jump > = 3 or roll > = 2 :
return 0
if n = = 0 :
return 1
if dp[n][roll][jump] ! = - 1 :
return dp[n][roll][jump]
w = solver(n - 1 , roll, 0 )
r = solver(n - 1 , roll + 1 , 0 )
j = solver(n - 1 , roll, jump + 1 )
dp[n][roll][jump] = (w + r + j) % mod
return dp[n][roll][jump]
def checkDistance(n):
return solver(n, 0 , 0 )
print (checkDistance( 6 ))
|
C#
using System;
class GFG
{
static int mod = ( int )(1e9 + 7);
static int [,,] dp = new int [100001, 2, 3];
static int Solver( int n, int roll, int jump)
{
if (jump >= 3 || roll >= 2)
{
return 0;
}
if (n == 0)
return 1;
if (dp[n, roll, jump] != -1)
return dp[n, roll, jump];
int w = Solver(n - 1, roll, 0);
int r = Solver(n - 1, roll + 1, 0);
int j = Solver(n - 1, roll, jump + 1);
return dp[n, roll, jump]
= ((w + r) % mod + j) % mod;
}
static int CheckDistance( int n)
{
for ( int i = 0; i < 100001; i++)
{
for ( int j = 0; j < 2; j++)
{
for ( int k = 0; k < 3; k++)
{
dp[i, j, k] = -1;
}
}
}
return Solver(n, 0, 0);
}
public static void Main( string [] args)
{
Console.WriteLine(CheckDistance(6));
}
}
|
Javascript
const mod = 1e9 + 7;
const dp = new Array(100001).fill( null ).map(() => new Array(2).fill( null ).map(() => new Array(3).fill(-1)));
function solver(n, roll, jump) {
if (jump >= 3 || roll >= 2) {
return 0;
}
if (n === 0) {
return 1;
}
if (dp[n][roll][jump] !== -1) {
return dp[n][roll][jump];
}
const w = solver(n - 1, roll, 0);
const r = solver(n - 1, roll + 1, 0);
const j = solver(n - 1, roll, jump + 1);
return dp[n][roll][jump] = ((w + r) % mod + j) % mod;
}
function checkDistance(n) {
for (let i = 0; i <= n; i++) {
for (let j = 0; j < 2; j++) {
for (let k = 0; k < 3; k++) {
dp[i][j][k] = -1;
}
}
}
return solver(n, 0, 0);
}
const result = checkDistance(6);
console.log(result);
|
Time Complexity: O(n), where ‘n’ is the total distance to be travelled.
Auxiliary Space: O(n*2*3) = O(n).
Share your thoughts in the comments
Please Login to comment...