Chief Hopper Discussions | | HackerRank

Chief Hopper

Sort by

recency

|

28 Discussions

|

  • + 0 comments

    Java O(n)

     public static int chiefHopper(List<Integer> arr) {
            int energy = 0;
    
            for (int i = arr.size() - 1; i >= 0; i--) {
                energy = (energy + arr.get(i) + 1) / 2;
            }
    
            return energy;
        }
    
  • + 0 comments
    public static int chiefHopper(List<Integer> arr){
                    ////////////////////
                    // Rather than iterating with the different inital energy to find the positive end energy, I should start with +ve end energy
                    // and iterate in the last to first to find the minimum positive energy.
    
                    // To calculate this, consider the end energy is 1.
                    // The energy before jumping the last building should be :
                    // newEnergy = currentEne + delta (currnetEne - height);
                    // newEnergy = 2 * currentEne - height;  => We need to have newEnergy >= 0 & need to find the currentEnergy.
    
                    // currentEne = newEnergy + height / 2;
    
                    double newEne = 0;
                    double currentEne = 0;
    
                    for(int i = arr.size() - 1; i >= 0; i--){
                            currentEne = Math.ceil( (newEne + arr.get(i)) / 2 );
                            newEne = currentEne;
                    }
    
                    return (int)currentEne;
            }
    
  • + 0 comments

    JS

    const chiefHopper = heights => heights.reduceRight((energy, height) => Math.ceil((energy + height) / 2), 0);
    
  • + 0 comments

    Python

    def chiefHopper(arr):
        energy = 0
        for a in arr[::-1]:
            energy = math.ceil((energy + a)/2)
        return energy
    
  • + 0 comments
    def chiefHopper(arr:list)->int:
        """
        LOGIC:
        We iterate the array from the end.
        If we check the formula, we got
        
        if bot energy is less than height
        newEnergy = botEnergy - (height - botEnergy) 
        and
        newEnergy = botEnergy +(botEnergy-height) if not.
        
        This two operations, when reduced get:
        newEnergy = 2*botEnergy - height
        So this is the operation we will perform from the back,
        keeping track of the result of each one to be used again
        in the inmediate before index.
    		For the initial value, we need to remember that, at the end, energy needs to be more than zero.
    		
    		
        """
        
        minimum = 0
        for height in reversed(arr):
            minimum = ceil((height+minimum)/2)
        return minimum