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;
}
Explanation:
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).