Jumping on the Clouds

There is a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. The player can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2 The player must avoid the thunderheads. Determine the minimum number of jumps it will take to jump from the starting postion to the last cloud. It is always possible to win the game.

For each game, you will get an array of clouds numbered  if they are safe or  if they must be avoided.


Example:

Input: n=7, arr[]={0,0,1,0,0,1,0}
Output: 4

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int jumpingOnClouds(vector<intarrint n)
{
    int cnt = 0;
    int i = 0jk;
    while (i < n)
    {
        j = i + 1;
        k = i + 2;
        if (arr[k] == 0)
        {
            i = i + 2;
            cnt++;
        }
        else
        {
            i = i + 1;
            cnt++;
        }
    }
    return cnt - 1;
}

int main()
{
    int n = 7;
    vector<intarr = {0010010};
    cout << jumpingOnClouds(arrn);
    return 0;
}


No comments:

Post a Comment