Chief Hopper


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.

def chiefHopper(arr):
    min_energy = 0

    for height in reversed(arr):
        min_energy = math.ceil((min_energy + height) / 2)

    return min_energy
function chiefHopper(arr) {
    let min_energy = 0

    for (let height of arr.reverse()) {
        min_energy = Math.ceil((min_energy + height) / 2)

    return min_energy

Join more than a thousand developers!

Subscribe now to our free, weekly e-mail with the best new articles, courses, and special bonuses.

We won't send you spam. Unsubscribe at any time.