Soluție HackerRank pentru Chief Hopper. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Chief Hopper
Cerinta completa
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 [Expresie matematică indisponibilă în copia arhivată] and at a height of [Expresie matematică indisponibilă în copia arhivată]. 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 [Expresie matematică indisponibilă în copia arhivată] is less than the height of the building, his [Expresie matematică indisponibilă în copia arhivată]
- If the bot’s [Expresie matematică indisponibilă în copia arhivată] is greater than the height of the building, his [Expresie matematică indisponibilă în copia arhivată]
Example
[Expresie matematică indisponibilă în copia arhivată]
Starting with [Expresie matematică indisponibilă în copia arhivată], 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 [Expresie matematică indisponibilă în copia arhivată] in this case is [Expresie matematică indisponibilă în copia arhivată].
Function Description
Complete the chiefHopper function in the editor below.
chiefHopper has the following parameter(s):
- int arr[n]: building heights
Returns
- int: the minimum starting [Expresie matematică indisponibilă în copia arhivată]
Input Format
The first line contains an integer [Expresie matematică indisponibilă în copia arhivată], the number of buildings.
The next line contains [Expresie matematică indisponibilă în copia arhivată] space-separated integers [Expresie matematică indisponibilă în copia arhivată], the heights of the buildings.
Constraints
- [Expresie matematică indisponibilă în copia arhivată]
- [Expresie matematică indisponibilă în copia arhivată] where [Expresie matematică indisponibilă în copia arhivată]
Sample Input 0
5
3 4 3 2 4
Sample Output 0
4
Explanation 0
If initial energy is 4, after step 1 energy is 5, after step 2 it’s 6, after step 3 it’s 9 and after step 4 it’s 16, finally at step 5 it’s 28.
If initial energy were 3 or less, the bot could not complete the course.
Sample Input 1
3
4 4 4
Sample Output 1
4
Explanation 1
In the second test case if bot has energy 4, it’s energy is changed by (4 – 4 = 0) at every step and remains 4.
Sample Input 2
3
1 6 4
Sample Output 2
3
Explanation 2
botEnergy height delta
3 1 +2
5 6 -1
4 4 0
4
We can try lower values to assure that they won’t work.
Limbajul de programare folosit: java8
Cod:
//Problem: https://www.hackerrank.com/challenges/chief-hopper
//Java 8
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int N = input.nextInt();
int[] buildings = new int[N];
int minEnergy = 0;
//Builds an array of the input
for(int i = 0; i < N; i++)
{
buildings[i] = input.nextInt();
}
//Goes through the input in reverse calculating the min energy required based on ending with 0 energy
for(int i = buildings.length-1; i >= 0; i--)
{
int buildingHeight = buildings[i];
if(buildingHeight > minEnergy)
{
minEnergy += (int) Math.ceil((buildingHeight - minEnergy) / 2.0);
}
else if(buildingHeight < minEnergy)
{
minEnergy = (int) Math.ceil((buildingHeight + minEnergy) / 2.0);
}
//Nothing occurs in the == case so we simply don't check for it
}
System.out.println(minEnergy);
}
}
Scor obtinut: 1.0
Submission ID: 464603324
Link challenge: https://www.hackerrank.com/challenges/chief-hopper/problem
