3308. Find Top Performing Driver
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:
- Initialize variables
maxScore
as -1 andtopDriver
as -1 to store the maximum performance score and the top performing driver's index. - Iterate through the list of drivers.
- Calculate the performance score for each driver using the given formula.
- If the current driver's performance score is greater than
maxScore
, updatemaxScore
andtopDriver
. - If the current driver's performance score is equal to
maxScore
, updatetopDriver
only if the current driver has a lower index. - 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.