diff --git a/DanielWagenerSolutions/implementation/bonAppetit.js b/DanielWagenerSolutions/implementation/bonAppetit.js new file mode 100644 index 0000000..e1e1105 --- /dev/null +++ b/DanielWagenerSolutions/implementation/bonAppetit.js @@ -0,0 +1,57 @@ +// Anna and Brian are sharing a meal at a restuarant and they agree to split the bill equally. Brian wants to order something that Anna is allergic to though, and they agree that Anna won't pay for that item. Brian gets the check and calculates Anna's portion. You must determine if his calculation is correct. + +// For example, assume the bill has the following prices: . Anna declines to eat item which costs . If Brian calculates the bill correctly, Anna will pay . If he includes the cost of , he will calculate . In the second case, he should refund to Anna. + +// Function Description + +// Complete the bonAppetit function in the editor below. It should print Bon Appetit if the bill is fairly split. Otherwise, it should print the integer amount of money that Brian owes Anna. + +// bonAppetit has the following parameter(s): + +// bill: an array of integers representing the cost of each item ordered +// k: an integer representing the zero-based index of the item Anna doesn't eat +// b: the amount of money that Anna contributed to the bill +// Input Format + +// The first line contains two space-separated integers and , the number of items ordered and the -based index of the item that Anna did not eat. +// The second line contains space-separated integers where . +// The third line contains an integer, , the amount of money that Brian charged Anna for her share of the bill. + +// Constraints + +// The amount of money due Anna will always be an integer +// Output Format + +// If Brian did not overcharge Anna, print Bon Appetit on a new line; otherwise, print the difference (i.e., ) that Brian must refund to Anna. This will always be an integer. + +// Sample Input 0 + +// 4 1 +// 3 10 2 9 +// 12 +// Sample Output 0 + +// 5 +// Explanation 0 +// Anna didn't eat item , but she shared the rest of the items with Brian. The total cost of the shared items is and, split in half, the cost per person is . Brian charged her for her portion of the bill. We print the amount Anna was overcharged, , on a new line. + +// Sample Input 1 + +// 4 1 +// 3 10 2 9 +// 7 +// Sample Output 1 + +// Bon Appetit +// Explanation 1 +// Anna didn't eat item , but she shared the rest of the items with Brian. The total cost of the shared items is and, split in half, the cost per person is . Because , we print Bon Appetit on a new line. + +function bonAppetit(bill, k, b) { + // Create an new array without bill[k], reduce that, then halve it: + const moneyAnnaOwes = + [...bill.slice(0, k), ...bill.slice(k + 1)].reduce( + (acc, val) => acc + val + ) / 2; + + console.log(b - moneyAnnaOwes || "Bon Appetit"); +} diff --git a/DanielWagenerSolutions/implementation/catAndMouse.js b/DanielWagenerSolutions/implementation/catAndMouse.js new file mode 100644 index 0000000..c967b41 --- /dev/null +++ b/DanielWagenerSolutions/implementation/catAndMouse.js @@ -0,0 +1,55 @@ +// Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat will reach the mouse first, assuming the mouse doesn't move and the cats travel at equal speed. If the cats arrive at the same time, the mouse will be allowed to move and it will escape while they fight. + +// You are given queries in the form of , , and representing the respective positions for cats and , and for mouse . Complete the function to return the appropriate answer to each query, which will be printed on a new line. + +// If cat catches the mouse first, print Cat A. +// If cat catches the mouse first, print Cat B. +// If both cats reach the mouse at the same time, print Mouse C as the two cats fight and mouse escapes. +// For example, cat is at position and cat is at . If mouse is at position , it is units from cat and unit from cat . Cat will catch the mouse. + +// Function Description + +// Complete the catAndMouse function in the editor below. It should return one of the three strings as described. + +// catAndMouse has the following parameter(s): + +// x: an integer, Cat 's position +// y: an integer, Cat 's position +// z: an integer, Mouse 's position +// Input Format + +// The first line contains a single integer, , denoting the number of queries. +// Each of the subsequent lines contains three space-separated integers describing the respective values of (cat 's location), (cat 's location), and (mouse 's location). + +// Constraints + +// Output Format + +// For each query, return Cat A if cat catches the mouse first, Cat B if cat catches the mouse first, or Mouse C if the mouse escapes. + +// Sample Input 0 + +// 2 +// 1 2 3 +// 1 3 2 +// Sample Output 0 + +// Cat B +// Mouse C +// Explanation 0 + +// Query 0: The positions of the cats and mouse are shown below: image + +// Cat will catch the mouse first, so we print Cat B on a new line. + +// Query 1: In this query, cats and reach mouse at the exact same time: image + +// Because the mouse escapes, we print Mouse C on a new line. + +function catAndMouse(x, y, z) { + return Math.abs(x - z) === Math.abs(y - z) + ? "Mouse C" + : Math.abs(x - z) < Math.abs(y - z) + ? "Cat A" + : "Cat B"; +} diff --git a/DanielWagenerSolutions/implementation/countingValleys.js b/DanielWagenerSolutions/implementation/countingValleys.js new file mode 100644 index 0000000..891fc8d --- /dev/null +++ b/DanielWagenerSolutions/implementation/countingValleys.js @@ -0,0 +1,62 @@ +// Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step he took, he noted if it was an uphill, , or a downhill, step. Gary's hikes start and end at sea level and each step up or down represents a unit change in altitude. We define the following terms: + +// A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level. +// A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level. +// Given Gary's sequence of up and down steps during his last hike, find and print the number of valleys he walked through. + +// For example, if Gary's path is , he first enters a valley units deep. Then he climbs out an up onto a mountain units high. Finally, he returns to sea level and ends his hike. + +// Function Description + +// Complete the countingValleys function in the editor below. It must return an integer that denotes the number of valleys Gary traversed. + +// countingValleys has the following parameter(s): + +// n: the number of steps Gary takes +// s: a string describing his path +// Input Format + +// The first line contains an integer , the number of steps in Gary's hike. +// The second line contains a single string , of characters that describe his path. + +// Constraints + +// Output Format + +// Print a single integer that denotes the number of valleys Gary walked through during his hike. + +// Sample Input + +// 8 +// UDDDUDUU +// Sample Output + +// 1 +// Explanation + +// If we represent _ as sea level, a step up as /, and a step down as \, Gary's hike can be drawn as: + +// _/\ _ +// \ / +// \/\/ +// He enters and leaves one valley. + +function countingValleys(n, s) { + // Step 1: Convert string to an array of 1's and -1's + + // Step 2: Reduce array. If the current elevation is zero and the current value is -1, increment valleyCount + + const { valleyCount } = s + .split("") + .map(val => (val === "U" ? 1 : -1)) + .reduce( + (acc, val) => ({ + elevation: acc.elevation + val, + valleyCount: + !acc.elevation && val === -1 ? acc.valleyCount + 1 : acc.valleyCount + }), + { elevation: 0, valleyCount: 0 } + ); + + return valleyCount; +} diff --git a/DanielWagenerSolutions/implementation/dayOfTheProgrammer.js b/DanielWagenerSolutions/implementation/dayOfTheProgrammer.js index e117b71..9fcbeab 100644 --- a/DanielWagenerSolutions/implementation/dayOfTheProgrammer.js +++ b/DanielWagenerSolutions/implementation/dayOfTheProgrammer.js @@ -1,3 +1,62 @@ +// Marie invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of the Programmer (the day of the year) during a year in the inclusive range from to . + +// From to , Russia's official calendar was the Julian calendar; since they used the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in , when the next day after January was February . This means that in , February was the day of the year in Russia. + +// In both calendar systems, February is the only month with a variable amount of days; it has days during a leap year, and days during all other years. In the Julian calendar, leap years are divisible by ; in the Gregorian calendar, leap years are either of the following: + +// Divisible by . +// Divisible by and not divisible by . +// Given a year, , find the date of the day of that year according to the official Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is . + +// For example, the given . is divisible by , so it is a leap year. The day of a leap year after is September 12, so the answer is . + +// Function Description + +// Complete the dayOfProgrammer function in the editor below. It should return a string representing the date of the day of the year given. + +// dayOfProgrammer has the following parameter(s): + +// year: an integer +// Input Format + +// A single integer denoting year . + +// Constraints + +// Output Format + +// Print the full date of Day of the Programmer during year in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is . + +// Sample Input 0 + +// 2017 +// Sample Output 0 + +// 13.09.2017 +// Explanation 0 + +// In the year , January has days, February has days, March has days, April has days, May has days, June has days, July has days, and August has days. When we sum the total number of days in the first eight months, we get . Day of the Programmer is the day, so then calculate to determine that it falls on day of the month (September). We then print the full date in the specified format, which is 13.09.2017. + +// Sample Input 1 + +// 2016 +// Sample Output 1 + +// 12.09.2016 +// Explanation 1 + +// Year is a leap year, so February has days but all the other months have the same number of days as in . When we sum the total number of days in the first eight months, we get . Day of the Programmer is the day, so then calculate to determine that it falls on day of the month (September). We then print the full date in the specified format, which is 12.09.2016. + +// Sample Input 2 + +// 1800 +// Sample Output 2 + +// 12.09.1800 +// Explanation 2 + +// Since 1800 is leap year. Day lies on 12 September. + function dayOfProgrammer(year) { const gregorian = year > 1918; const switchover = year === 1918; diff --git a/DanielWagenerSolutions/implementation/drawingBook.js b/DanielWagenerSolutions/implementation/drawingBook.js new file mode 100644 index 0000000..5ed6ffc --- /dev/null +++ b/DanielWagenerSolutions/implementation/drawingBook.js @@ -0,0 +1,80 @@ +// Brie’s Drawing teacher asks her class to open their books to a page number. Brie can either start turning pages from the front of the book or from the back of the book. She always turns pages one at a time. When she opens the book, page is always on the right side: + +// image + +// When she flips page , she sees pages and . Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book. If the book is pages long, and she wants to turn to page , what is the minimum number of pages she will turn? She can start at the beginning or the end of the book. + +// Given and , find and print the minimum number of pages Brie must turn in order to arrive at page . + +// Function Description + +// Complete the pageCount function in the editor below. It should return the minimum number of pages Brie must turn. + +// pageCount has the following parameter(s): + +// n: the number of pages in the book +// p: the page number to turn to +// Input Format + +// The first line contains an integer , the number of pages in the book. +// The second line contains an integer, , the page that Brie's teacher wants her to turn to. + +// Constraints + +// Output Format + +// Print an integer denoting the minimum number of pages Brie must turn to get to page . + +// Sample Input 0 + +// 6 +// 2 +// Sample Output 0 + +// 1 +// Explanation 0 + +// If Brie starts turning from page , she only needs to turn page: + +// Untitled Diagram(6).png + +// If Brie starts turning from page , she needs to turn pages: + +// Untitled Diagram(3).png + +// Because we want to print the minumum number of page turns, we print as our answer. + +// Sample Input 1 + +// 5 +// 4 +// Sample Output 1 + +// 0 +// Explanation 1 + +// If Brie starts turning from page , she needs to turn pages: + +// Untitled Diagram(4).png + +// If Brie starts turning from page , she doesn't need to turn any pages: + +// Untitled Diagram(5).png + +// Because we want to print the minimum number of page turns, we print as our answer. + +function pageCount(n, p) { + const pagesFromFront = Math.ceil((p - 1) / 2); + + // This part works because of the way it is + // Like, I just tinkered with it until it passed the test cases + const pagesFromBack = (() => { + if (n === p) return 0; + if (n % 2) { + return Math.ceil((n - p - 1) / 2); + } + return Math.floor((n - p) / 2) || 1; + })(); + + return pagesFromFront < pagesFromBack ? pagesFromFront : pagesFromBack; +} diff --git a/DanielWagenerSolutions/implementation/electronicsShop.js b/DanielWagenerSolutions/implementation/electronicsShop.js new file mode 100644 index 0000000..ccbb856 --- /dev/null +++ b/DanielWagenerSolutions/implementation/electronicsShop.js @@ -0,0 +1,79 @@ +// Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the items, given her budget. + +// Given the price lists for the store's keyboards and USB drives, and Monica's budget, find and print the amount of money Monica will spend. If she doesn't have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items. + +// For example, suppose she has to spend. Three types of keyboards cost . Two USB drives cost . She could purchase a , or a . She chooses the latter. She can't buy more than items so she can't spend exactly . + +// Function Description + +// Complete the getMoneySpent function in the editor below. It should return the maximum total price for the two items within Monica's budget, or if she cannot afford both items. + +// getMoneySpent has the following parameter(s): + +// keyboards: an array of integers representing keyboard prices +// drives: an array of integers representing drive prices +// b: the units of currency in Monica's budget +// Input Format + +// The first line contains three space-separated integers , , and , her budget, the number of keyboard models and the number of USB drive models. +// The second line contains space-separated integers , the prices of each keyboard model. +// The third line contains space-separated integers , the prices of the USB drives. + +// Constraints + +// The price of each item is in the inclusive range . +// Output Format + +// Print a single integer denoting the amount of money Monica will spend. If she doesn't have enough money to buy one keyboard and one USB drive, print -1 instead. + +// Sample Input 0 + +// 10 2 3 +// 3 1 +// 5 2 8 +// Sample Output 0 + +// 9 +// Explanation 0 + +// She can buy the keyboard and the USB drive for a total cost of . + +// Sample Input 1 + +// 5 1 1 +// 4 +// 5 +// Sample Output 1 + +// -1 +// Explanation 1 + +// There is no way to buy one keyboard and one USB drive because , so we print . + +function getMoneySpent(keyboards, drives, b) { + // Return early if Monica can't buy nothin' + if (Math.min(...keyboards) >= b || Math.min(...drives) >= b) { + return -1; + } + + let expensiveCombinations = []; + + keyboards = keyboards.sort((val1, val2) => val2 - val1); + drives = drives.sort((val1, val2) => val2 - val1); + + for (let i = 0; i < keyboards.length; i++) { + for (let j = 0; j < drives.length; j++) { + expensiveCombinations.push(keyboards[i] + drives[j]); + } + } + + return expensiveCombinations + .sort((val1, val2) => val2 - val1) + .reduce((acc, val) => { + if (val <= b && val > acc) { + return val; + } + + return acc; + }, -1); +} diff --git a/DanielWagenerSolutions/implementation/formingAMagicSquare.js b/DanielWagenerSolutions/implementation/formingAMagicSquare.js new file mode 100644 index 0000000..20ccf22 --- /dev/null +++ b/DanielWagenerSolutions/implementation/formingAMagicSquare.js @@ -0,0 +1,98 @@ +// We define a magic square to be an matrix of distinct positive integers from to where the sum of any row, column, or diagonal of length is always equal to the same number: the magic constant. + +// You will be given a matrix of integers in the inclusive range . We can convert any digit to any other digit in the range at cost of . Given , convert it into a magic square at minimal cost. Print this cost on a new line. + +// Note: The resulting magic square must contain distinct integers in the inclusive range . + +// For example, we start with the following matrix : + +// 5 3 4 +// 1 5 8 +// 6 4 2 +// We can convert it to the following magic square: + +// 8 3 4 +// 1 5 9 +// 6 7 2 +// This took three replacements at a cost of . + +// Function Description + +// Complete the formingMagicSquare function in the editor below. It should return an integer that represents the minimal total cost of converting the input square to a magic square. + +// formingMagicSquare has the following parameter(s): + +// s: a array of integers +// Input Format + +// Each of the lines contains three space-separated integers of row . + +// Constraints + +// Output Format + +// Print an integer denoting the minimum cost of turning matrix into a magic square. + +// Sample Input 0 + +// 4 9 2 +// 3 5 7 +// 8 1 5 +// Sample Output 0 + +// 1 +// Explanation 0 + +// If we change the bottom right value, , from to at a cost of , becomes a magic square at the minimum possible cost. + +// Sample Input 1 + +// 4 8 2 +// 4 5 7 +// 6 1 6 +// Sample Output 1 + +// 4 +// Explanation 1 + +// Using 0-based indexing, if we make + +// -> at a cost of +// -> at a cost of +// -> at a cost of , +// then the total cost will be . + +function formingMagicSquare(s) { + // A normal 3x3 magic square has only two possible sequences for its perimeter: the magicSequence defined below and its reverse. As such, the magic square has only 8 possible perimeters. Why not 16 possible perimeters? Because, the magic square will never work with an odd number in a corner. The corners always have to be 8, 4, 2, and 6 (or the reverse of that), in that sequence but starting from any of those four numbers. + const magicSequence = [8, 3, 4, 9, 2, 7, 6, 1]; + const magicSequenceReverse = [...magicSequence].reverse(); + // Since the first value of this array represents a corner value, we have to shift the 1: + magicSequenceReverse.push(magicSequenceReverse.shift()); + + // With that set up, we can generate all eight magic perimeters. + let magicPerimeters = []; + for (let i = 0; i <= 8; i++) { + // Only push on even iterations. This gives us the effect of rearranging each array twice before each push. + if (!(i % 2)) { + magicPerimeters.push([...magicSequence], [...magicSequenceReverse]); + } + + magicSequence.push(magicSequence.shift()); + magicSequenceReverse.push(magicSequenceReverse.shift()); + } + + // Get the input square's current perimeter: + let squarePerimeter = [...s[0], s[1][2], ...s[2].reverse(), s[1][0]]; + + // Now, find the costs of changing our perimeter into each of the eight magic perimeters. Get the cheapest one, and then add that to the cost of changing the middle sqaure to 5 (the middle square in a normal 3x3 magic square is always 5) + return ( + Math.min( + ...magicPerimeters.map(magicPerimeter => + magicPerimeter.reduce( + (acc, val, index) => acc + Math.abs(val - squarePerimeter[index]), + 0 + ) + ) + ) + Math.abs(5 - s[1][1]) + ); +} diff --git a/DanielWagenerSolutions/implementation/pickingNumbers.js b/DanielWagenerSolutions/implementation/pickingNumbers.js new file mode 100644 index 0000000..72925f4 --- /dev/null +++ b/DanielWagenerSolutions/implementation/pickingNumbers.js @@ -0,0 +1,76 @@ +// Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to . For example, if your array is , you can create two subarrays meeting the criterion: and . The maximum length subarray has elements. + +// Function Description + +// Complete the pickingNumbers function in the editor below. It should return an integer that represents the length of the longest array that can be created. + +// pickingNumbers has the following parameter(s): + +// a: an array of integers +// Input Format + +// The first line contains a single integer , the size of the array . +// The second line contains space-separated integers . + +// Constraints + +// The answer will be . +// Output Format + +// A single integer denoting the maximum number of integers you can choose from the array such that the absolute difference between any two of the chosen integers is . + +// Sample Input 0 + +// 6 +// 4 6 5 3 3 1 +// Sample Output 0 + +// 3 +// Explanation 0 + +// We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference (i.e., and ), so we print the number of chosen integers, , as our answer. + +// Sample Input 1 + +// 6 +// 1 2 2 3 1 2 +// Sample Output 1 + +// 5 +// Explanation 1 + +// We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference (i.e., , , and ), so we print the number of chosen integers, , as our answer. + +function pickingNumbers(a) { + let objectOfArrays = a + .sort((currentVal, nextVal) => currentVal - nextVal) + .reduce((acc, val) => { + // Does an array exist at the key equivalent to this value? If so, push to that array: + if (acc[val]) { + acc[val] = [...acc[val], val]; + } + + // If this value does not have a key in the accumulator, make one: + if (!acc[val]) { + acc[val] = [val]; + } + + // Does a key of value minus 1 exist in the accumulator? If so, push to that array: + if (acc[val - 1]) { + acc[val - 1] = [...acc[val - 1], val]; + } + + return acc; + }, {}); + + // Find the longest array in objectOfArrays + let longestArrayLength = 0; + + for (const array in objectOfArrays) { + if (objectOfArrays[array].length > longestArrayLength) { + longestArrayLength = objectOfArrays[array].length; + } + } + + return longestArrayLength; +} diff --git a/DanielWagenerSolutions/implementation/pickingNumbersAbrieInspired.js b/DanielWagenerSolutions/implementation/pickingNumbersAbrieInspired.js new file mode 100644 index 0000000..1acfd90 --- /dev/null +++ b/DanielWagenerSolutions/implementation/pickingNumbersAbrieInspired.js @@ -0,0 +1,63 @@ +// Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to . For example, if your array is , you can create two subarrays meeting the criterion: and . The maximum length subarray has elements. + +// Function Description + +// Complete the pickingNumbers function in the editor below. It should return an integer that represents the length of the longest array that can be created. + +// pickingNumbers has the following parameter(s): + +// a: an array of integers +// Input Format + +// The first line contains a single integer , the size of the array . +// The second line contains space-separated integers . + +// Constraints + +// The answer will be . +// Output Format + +// A single integer denoting the maximum number of integers you can choose from the array such that the absolute difference between any two of the chosen integers is . + +// Sample Input 0 + +// 6 +// 4 6 5 3 3 1 +// Sample Output 0 + +// 3 +// Explanation 0 + +// We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference (i.e., and ), so we print the number of chosen integers, , as our answer. + +// Sample Input 1 + +// 6 +// 1 2 2 3 1 2 +// Sample Output 1 + +// 5 +// Explanation 1 + +// We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference (i.e., , , and ), so we print the number of chosen integers, , as our answer. + +function pickingNumbers(a) { + return a + .map((_, i) => { + let copy = [...a]; + return [...copy.splice(i, 1), ...copy].filter( + (el, _, target) => target[0] >= el && target[0] - el <= 1 + ); + }) + .reduce((acc, val) => (val.length > acc ? val.length : acc), 0); + + // Slightly more ridiculous solution: + return Math.max( + ...a.map((_, i) => { + let copy = [...a]; + return [...copy.splice(i, 1), ...copy].filter( + (el, _, target) => target[0] >= el && target[0] - el <= 1 + ).length; + }) + ); +} diff --git a/DanielWagenerSolutions/implementation/theHurdleRace.js b/DanielWagenerSolutions/implementation/theHurdleRace.js new file mode 100644 index 0000000..f80771d --- /dev/null +++ b/DanielWagenerSolutions/implementation/theHurdleRace.js @@ -0,0 +1,58 @@ +// 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 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 , and an initial maximum height Dan can jump, , determine the minimum number of doses Dan must take to be able to clear all the hurdles in the race. + +// For example, if and Dan can jump unit high naturally, he must take 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 and , the number of hurdles and the maximum height Dan can jump naturally. +// The second line contains space-separated integers where . + +// Constraints + +// Output Format + +// Print an integer denoting the minimum doses of magic potion Dan must drink to complete the hurdle race. + +// Sample Input 0 + +// 5 4 +// 1 6 3 5 2 +// Sample Output 0 + +// 2 +// Explanation 0 + +// Dan's character can jump a maximum of units, but the tallest hurdle has a height of : + +// image + +// To be able to jump all the hurdles, Dan must drink doses. + +// Sample Input 1 + +// 5 7 +// 2 5 4 5 2 +// Sample Output 1 + +// 0 +// Explanation 1 + +// Dan's character can jump a maximum of units, which is enough to cross all the hurdles: + +// image + +// Because he can already jump all the hurdles, Dan needs to drink doses. + +function hurdleRace(k, height) { + return Math.max(...height) > k ? Math.max(...height) - k : 0; +}