Chief Hopper

Chief Hopper - A HackerRank challenge solution

Problem

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
48

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.

Si te gusta el contenido, por favor apoya mi trabajo!

Juan Cruz Martinez - Author @ Live Code Stream

Juan Cruz Martinez

¡Hola!

Soy Juan Cruz Martinez, el fundador de LiveCodeStream.dev. Me encanta programar y creo en el poder de la programación no solo para construir un mundo mejor, sino para hacer que TU vida sea mejor.

Fundé LiveCodeStream.dev porque quería ayudarte a aprender a programar, construir una mejor carrera y, en última instancia, crear una vida mejor. Eso me sucedió hace más de una década cuando comencé a programar, y le sucede a decenas de miles de personas a diario. Quiero que TÚ te unas a esa revolución.

No importa cómo lo llames, escribir código, programación, desarrollo de software o cualquier otra cosa, las habilidades involucradas están en una demanda cada vez mayor. Y ya sea que quieras incursionar en el desarrollo de sitios web, diseño de juegos, desarrollo de blockchain o cualquier otra cosa, quiero ayudarte.