Problem · Sorting

Get Maximum Efficiency

Learn this problem
MediumExpediaINTERNOA

Problem statement

When evaluating a machine learning model, n test cases are provided. Each is associated with an arrivalTime[i] indicating the times each test case was given. The testing environment is activated once for some time, then enters an inactive state. If activated at time t1 and deactivated at time t2, tests with arrival times between t1 and t2 (inclusive) are executed. The total time the system was active is t2 - t1.

Efficiency of testing system = number of test cases tested - total time the system was active.

Determine the maximum efficiency when the testing environment selects optimum activation and deactivation times. The environment must execute at least two test cases during its active period. Return the maximum possible efficiency.

Notes:

  • Execution time for test cases is practically instantaneous; that is, it takes almost no time.
  • If two test cases share the same arrival time, they are evaluated simultaneously.
  • Efficiency may take negative values.

Function

getMaxEfficiency(arrivalTime: int[]) → int

Complete the function getMaxEfficiency in the editor below.

getMaxEfficiency takes the following parameter(s):

  • int arrivalTime[n]: the arrival time of each prompt

Returns

int: the maximum efficiency possible

⊹₊ ˚‧︵‿₊୨Credit to Charlotte୧₊‿︵‧ ˚ ₊⊹

Examples

Example 1

arrivalTime = [9, 1, 3, 5, 6]return = 1
Example 1 illustration

It is optimal to choose t1 = 5 and t2 = 6. If the testing environment is active from t = 5 to 6, the 4th and 5th test cases will be executed. The number of test cases tested = 2 and the time the system was active is 6 - 5 = 1. The efficiency of the system is 2 - 1 = 1.

Return 1 as the answer.

Example 2

arrivalTime = [4, 2, 1]return = 1

Choose t1 = 1 and t2 = 2. If the system is active from t = 1 to 2, test cases 1 and 2 will be tested. The efficiency of the system is 2 - 1 = 1.

Constraints

  • 2 ≤ n ≤ 2 * 10^5
  • 1 ≤ arrivalTime[i] ≤ 10^9

More Expedia problems

drafts saved locally
public int getMaxEfficiency(int[] arrivalTime) {
  // write your code here
}
arrivalTime[9, 1, 3, 5, 6]
expected1
checking account