[Solved] Hurdle Race with Java, CPP, Python

Hurdle Race: You are participating in a hurdle race. The race comprises of stairs of varying height and there are points that can be collected after every meter. Initially, you are at height 0 and meter 0.

To make the race interesting, there are certain checkpoints in the race and at each checkpoint, you need to be at a specific minimum height. You have to finish the race with the maximum number of points.

You are provided with 4 arrays-
Points :- Denotes the points that can be collected after every meter.
Heights :- Denotes the height of stairs at every meter.
Checkpoints :- Denotes the various checkpoints in meters, at which you must have a minimum height to proceed ahead in the race.
MinimumHeight :- Denotes the minimum height required at the corresponding checkpoint.
After every meter, you can either pick up the points or climb the stairs to increase your height.
Your task is to return the maximum points that can be collected when you reach the end of the race or return -1 if you cannot reach the end of the race.

Input Specification:
input1: An array of integers denoting Points.
input2: An integer denoting the length of the array Points.
input3: An array of integers denoting Heights.
input4: An integer denoting the length of the array Heights.
input5: An array of integers denoting Checkpoints.
input6: An integer denoting the length of the array Checkpoints.
input7: An array of integers denoting MinimumHeight.
input8: An integer denoting the length of the array MinimumHeight.

Output Specification:
You must return an integer denoting the maximum points that can be collected when you reach the end of the race.

Example 1:
input1: {1,4,3,2}
input2: 4
input3: {4,1,2,3}
input4: 4
input5: {1,2}
input6: 2
input7:{3,2}
input8: 2

Output: 9

Solution

Steps:

  1. Initialize some variables to keep track of your progress and points:
    • height: Initialize to 0 (your starting height).
    • max_points: Initialize to 0 (total points collected).
  2. Iterate through the checkpoints using a for loop:
    • Check if your current height is less than the height required at the next checkpoint (checkpoints[0]). If it is, continue to the next step; otherwise, skip to the next checkpoint.
    • While your height is less than the required height at the checkpoint, check if the next step’s height is higher than your current height (heights[i] > height). If it is, update your height to the new height (height = heights[i]).
    • After you have reached the required height at the checkpoint, start collecting points by adding the current point value (points[i]) to max_points.
  3. Once you have passed the first checkpoint, continue with the second checkpoint in a similar manner:
    • Check if your current height is less than the height required at the second checkpoint (checkpoints[1]). If it is, continue to the next step; otherwise, skip to the next checkpoint.
    • While your height is less than the required height at the checkpoint, check if the next step’s height is higher than your current height (heights[i] > height). If it is, update your height to the new height (height = heights[i]).
    • After you have reached the required height at the checkpoint, start collecting points by adding the current point value (points[i]) to max_points.
  4. Once you have iterated through all checkpoints, if your current height is still below the required height at the last checkpoint (checkpoints[1]), return -1 to indicate that you cannot reach the end of the race.
  5. Otherwise, return the maximum points collected max_points.
public static int hurdleRace(int[] input1, int input2, int[] input3, int input4, int[] input5, int input6, int[] input7, int input8) {
    int[] points = input1;
    int[] heights = input3;
    int[] checkpoints = input5;
    int[] minHeights = input7;
    int maxPoints = 0;
    int height = 0;

    for (int i = 1; i < input2; i++) {
        if (height < checkpoints[0]) {
            // github.com/sauravhathi
            if (heights[i] > height) {
                height = heights[i];
            }
            if (height >= checkpoints[0]) {
                maxPoints += points[i];
            }
        } else {
            if (heights[i] > height) {
                height = heights[i];
            }
            if (height >= checkpoints[1]) {
                maxPoints += points[i];
            }
        }
    }

    if (height < checkpoints[1]) {
        return -1;
    }

    return maxPoints;
}
int hurdleRace(vector<int> input1, int input2, vector<int> input3, int input4, vector<int> input5, int input6, vector<int> input7, int input8) {
    vector<int> points = input1;
    vector<int> heights = input3;
    vector<int> checkpoints = input5;
    vector<int> minHeights = input7;
    int maxPoints = 0;
    int height = 0;

    for (int i = 1; i < input2; i++) {
        if (height < checkpoints[0]) {

               // github.com/sauravhathi
            if (heights[i] > height) {
                height = heights[i];
            }
            if (height >= checkpoints[0]) {
                maxPoints += points[i];
            }
        } else {
            if (heights[i] > height) {
                height = heights[i];
            }
            if (height >= checkpoints[1]) {
                maxPoints += points[i];
            }
        }
    }

    if (height < checkpoints[1]) {
        return -1;
    }

    return maxPoints;
}
def hurdleRace(input1,input2,input3,input4,input5,input6,input7,input8):
    # Write code here
    points = input1
    heights = input3
    checkpoints = input5
    min_heights = input7
    max_points = 0
    height = 0
    for i in range(1,len(points)):
        if height < checkpoints[0]:
            if heights[i] > height:
                height = heights[i]
            if height >= checkpoints[0]:
                max_points += points[i]
            # github.com/sauravhathi
        else:
            if heights[i] > height:
                height = heights[i]
            if height >= checkpoints[1]:
                max_points += points[i]
    if height < checkpoints[1]:
        return -1
    return max_points

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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