알고리즘 단련장/해커랭크

[해커랭크] The Hurdle Race

snapcoder 2025. 8. 14. 17:48
728x90
반응형
SMALL

[해커랭크] The Hurdle Race

https://www.hackerrank.com/challenges/the-hurdle-race/problem?isFullScreen=true

 

The Hurdle Race | HackerRank

Determine the maximum value in an array less a value, limit >= 0.

www.hackerrank.com

 

A video player plays a game in which the character competes in a hurdle race. Hurdles are of varying heights, and the characters have a maximum height they can jump. There is a magic potion they can take that will increase their maximum jump height by  unit for each dose. How many doses of the potion must the character take to be able to jump all of the hurdles. If the character can already clear all of the hurdles, return .

Example

The character can jump  unit high initially and must take  doses of potion to be able to jump all of the hurdles.

Function Description

Complete the hurdleRace function in the editor below.

hurdleRace has the following parameter(s):

  • int k: the height the character can jump naturally
  • int height[n]: the heights of each hurdle

Returns

  • int: the minimum number of doses required, always  or more

Input Format

The first line contains two space-separated integers  and , the number of hurdles and the maximum height the character can jump naturally.
The second line contains  space-separated integers  where .

Constraints

  •  
  •  

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 :

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:

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

 

 

 

 

풀이

그저 최대값 구해서 k와 빼주면 된다.

음수가 되면 0으로 반환.

문제는 긴데 풀이법이 이래도 되나 싶을 정도로 너무 쉬웠던 문제.

 

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'hurdleRace' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER k
     *  2. INTEGER_ARRAY height
     */

    public static int hurdleRace(int k, List<Integer> height) {
    // Write your code here
        Integer maxH = Collections.max(height);
        return maxH - k > 0 ? maxH - k : 0;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int n = Integer.parseInt(firstMultipleInput[0]);

        int k = Integer.parseInt(firstMultipleInput[1]);

        List<Integer> height = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.hurdleRace(k, height);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

 

 

728x90
반응형
LIST