Jumping on the Clouds Algorithm

"What happens on cloud nine...?"

Β·

5 min read

Konnichiwa fellow programmers and welcome to another algorithm session!πŸ™‚

In this article, I'll be describing how I solved the Jumping on the Clouds Challenge on HackerRank using Python 3.

This challenge is part of the Implementation challenges in the Algorithm section on HackerRank.

If this is your first time reading my data structures and algorithms article, consider reading more in my Algos in Plain English series.πŸ˜ƒ

Problem 🎲

A child is playing a cloud hopping game. In this game, there are sequentially numbered clouds that can be thunderheads or cumulus clouds. The character must jump from cloud to cloud until it reaches the start again.

There is an array of clouds, c and an energy level e = 100. The character starts from c[0] and uses 1 unit of energy to make a jump of size k to cloud c[(i + k) % n]. If it lands on a thundercloud, c[i] = 1, its energy (e) decreases by 2 additional units. The game ends when the character lands back on cloud 0.

Given the values of n, k, and the configuration of the clouds as an array c, determine the final value of e after the game ends.

Example

c = [0, 0, 1, 0]

The indices of the path are 0 -> 2 -> 0. The energy level reduces by 1 for each jump to 98. The character landed on one thunderhead at an additional cost of 2 energy units. The final energy level is 96.

Note: Recall that % refers to the modulo operation. In this case, it serves to make the route circular. If the character is at c[n - 1] and jumps 1, it will arrive at c[0].

Function Description

Complete the jumpingOnClouds function in the editor below.

jumpingOnClouds has the following parameter(s):

  • int c[n]: the cloud types along the path
  • int k: the length of one jump

Returns

  • int: the energy level remaining.

Sample Input:

c = [0, 0, 1, 0, 0, 1, 1, 0]
k = 2

Sample Output

92

Explanation

In the diagram below, red clouds are thunderheads and purple clouds are cumulus clouds:

image.png

Observe that our thunderheads are the clouds numbered 2, 5, and 6. The character makes the following sequence of moves:

  1. Move: 0 -> 2, Energy: e = 100 - 1 - 2 = 97.
  2. Move: 2 -> 4, Energy: e = 97 - 1 = 96.
  3. Move: 4 -> 6, Energy: e = 96 - 1 - 2 = 93.
  4. Move: 6 -> 0, Energy: e = 93 - 1 = 92.

Check out the original challenge on HackerRank for more info on input constraints and other sample inputs.

Solution πŸ”Ž

In plain English, the 'energy' of a character who jumps on clouds reduces by 1 on stepping on each cloud, and reduces by an extra 2 if it's a 'thundercloud'. Given a number of clouds, we need to get the remaining energy after k number of jumps.

BuenaNocheGIF.gif

Let's write down the steps we'll use to solve this problem.

# While the character is not back on the first cloud.
# Reduce the energy, e by 1 for a default 'jump' and by an extra 2 if it's a thundercloud.
# Get the new position after a 'jump'.
# Break out of the loop and return the energy once the character lands on the first cloud.

Step 1️⃣: Initialising variables 🏁.

We'll create variables to track and better understand the control flow and logic.

  1. We track the current position of the character in the cloud_index variable
  2. We represent the first cloud with the first_cloud_index variable.
    def jumpingOnClouds(c, k):
     n = len(c)
     energy = 100
     cloud_index = 0
     first_cloud_index = 0
    

Step 2️⃣: Create the 'jumping' logic 🦘.

  1. The character starts jumping from energy, e = 100 and continues to jump until it lands on the first cloud. From this we can easily create a while loop that only breaks when the character is on the first cloud.
  2. The energy is reduced by 1 for each jump and reduced by an additional jump if the current cloud on which the character lands is a thundercloud represented by current_cloud == 1.
  3. At the end of the jump, we get the current position represented by (cloud_index + k) % n

    def jumpingOnClouds(c, k):
     n = len(c)
     energy = 100
     cloud_index = 0
     first_cloud_index = 0
    
     while cloud_index is not first_cloud_index or energy == 100:
         energy -= 1
         current_cloud = c[cloud_index]
         if current_cloud == 1:
             energy -= 2
    
         cloud_index = (cloud_index + k) % n
    

Step 3️⃣: Return the energy at the end of the jumps ☁️🦘.

def jumpingOnClouds(c, k):
    n = len(c)
    energy = 100
    cloud_index = 0
    first_cloud_index = 0

    while cloud_index is not first_cloud_index or energy == 100:
        energy -= 1
        current_cloud = c[cloud_index]
        if current_cloud == 1:
            energy -= 2

        cloud_index = (cloud_index + k) % n

    return energy

Initially, I had misunderstood the problem by iterating through the clouds themselves, which caused unexpected results. If you chose to iterate, iterating through the jumps would have been appropriate.

That's it! Thanks for reading! πŸ™‚

Hope this article helped with understanding this challenge. Happy Coding!πŸ‘¨πŸ½β€πŸ’»

NonsocchiπŸ₯·πŸ½

Buy me a coffee.png

Β