Hackerrank The Hurdle Race (C++)

C++ :

int hurdleRace(int k, vector<int> height) {
    int drinkTimes = 0, maxHeight = height[0];

    for(int i = 1; i < height.size(); i++){
        if(height[i] > maxHeight){
            maxHeight = height[i];

    drinkTimes = maxHeight - k;
    if(drinkTimes < 0){
        drinkTimes = 0;

    return drinkTimes;


First, find the max height of the hurdles in the race. Assign it to maxHeight. Then find the number of times Dan has to drink the potion by maxHeight – k.

If Dan’s min jump k can already jump over all the hurdles, Dan does not need to drink the potion (assign drinkTimes to 0).

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *