Chief Hopper

Chief Hopper - A HackerRank challenge solution


Chief’s bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building 0 and at a height of 0. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero.

Units of height relate directly to units of energy. The bot’s energy level is calculated as follows:

  • If the bot’s botEnergy is less than the height of the building, his new_energy = bot_energy - (height - bot_energy)
  • If the bot’s botEnergy is greater than the height of the building, his new_energy = bot_energy + (bot_energy -height)

For example, building heights are given as h = [2, 3, 4, 3, 2]. If the bot starts with botEnergy = 4, we get the following table:

botEnergy   height  delta
4               2       +2
6               3       +3
9               4       +5
14              3       +11
25              2       +23

That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting in this case is 3.

Read more on the challenge page…

Problem Analysis and Solution

My Solution

I’m providing the solution for Python and JS, please leave on the comments if you found a better way.

If you liked what you saw, please support my work!

Juan Cruz Martinez - Author @ Live Code Stream

Juan Cruz Martinez

Juan has made it his mission to help aspiring developers unlock their full potential. With over two decades of hands-on programming experience, he understands the challenges and rewards of learning to code. By providing accessible and engaging educational content, Juan has cultivated a community of learners who share their passion for coding. Leveraging his expertise and empathetic teaching approach, Juan has successfully guided countless students on their journey to becoming skilled developers, transforming lives through the power of technology.