The Hurdle Race

The Hurdle Race

Dan is playing a video game in which his character competes in a hurdle race. Hurdles are of varying heights, and Dan has a maximum height he can jump. There is a magic potion he can take that will increase his maximum height by 1 unit for each dose. How many doses of the potion must he take to be able to jump all of the hurdles.

Given an array of hurdle heights height, and an initial maximum height Dan can jump, k, determine the minimum number of doses Dan must take to be able to clear all the hurdles in the race.

For example, if height = [1,2,3,3,2] and Dan can jump 1 unit high naturally, he must take 3 - 1 = 2 doses of potion to be able to jump all of the hurdles.

Function Description

Complete the hurdleRace function in the editor below. It should return the minimum units of potion Dan needs to drink to jump all of the hurdles.

hurdleRace has the following parameter(s):

  • k: an integer denoting the height Dan can jump naturally
  • height: an array of integers denoting the heights of each hurdle

Input Format

The first line contains two space-separated integers n and k, the number of hurdles and the maximum height Dan can jump naturally.

The second line contains n space-separated integers height[i] where 0 <= i < n.

Constraints

  • 1 <= n,k <= 100
  • 1 <= height[i] <= 100

Output Format

Print an integer denoting the minimum doses of magic potion Dan must drink to complete the hurdle race.

Sample Input 0

1
2
5 4
1 6 3 5 2

Sample Output 0

1
2

Explanation 0

Dan’s character can jump a maximum of k = 4 units, but the tallest hurdle has a height of h1 = 6:

To be able to jump all the hurdles, Dan must drink 6 - 4 = 2 doses.

Sample Input 1

1
2
5 7
2 5 4 5 2

Sample Output 1

1
0

Explanation 1

Dan’s character can jump a maximum of k = 7 units, which is enough to cross all the hurdles:

Because he can already jump all the hurdles, Dan needs to drink 0 doses.


Solution

1
2
3
4
5
6
7
8
9
// Complete the hurdleRace function below.
function hurdleRace(k, height) {
return (height || []).reduce((target, hurdle) => {
(hurdle > k) && (target += hurdle - k);
(hurdle > k) && (k += hurdle - k);

return target;
}, 0);
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×