Chief Hopper Discussions | | HackerRank

Chief Hopper

  • + 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;
            }