Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

3308. Find Top Performing Driver

Database

Explanation:

To find the top performing driver, we can iterate through the given list of drivers and calculate their performance score based on the given formula. We will keep track of the maximum performance score and the driver with the highest performance score. In case of ties, we will choose the driver with the lower index.

Algorithm:

  1. Initialize variables maxScore as -1 and topDriver as -1 to store the maximum performance score and the top performing driver's index.
  2. Iterate through the list of drivers.
  3. Calculate the performance score for each driver using the given formula.
  4. If the current driver's performance score is greater than maxScore, update maxScore and topDriver.
  5. If the current driver's performance score is equal to maxScore, update topDriver only if the current driver has a lower index.
  6. Return the topDriver as the top performing driver.

Time Complexity:

The time complexity of this solution is O(N), where N is the number of drivers in the input list.

Space Complexity:

The space complexity of this solution is O(1) as we are using constant extra space.: :

public int findTopPerformingDriver(int[] driverScores) {
    int maxScore = -1;
    int topDriver = -1;

    for (int i = 0; i < driverScores.length; i++) {
        int score = driverScores[i] * (i + 1);
        if (score > maxScore || (score == maxScore && i < topDriver)) {
            maxScore = score;
            topDriver = i;
        }
    }

    return topDriver;
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.