Problem · Array

Regulate Temperatures

Learn this problem
HardAmazonNEW GRADOA
See Amazon hiring insights

Problem statement

There's a computer system owned by an Amazon user. This system is equipped with n CPU cores, each with a specific temperature denoted by temperature[].

The user, let's call them the Optimizer, is keen on enhancing the performance of their machine by regulating the core temperatures. In a single operation, the Optimizer can choose one of the following techniques:

  • Select a position i (0 ≤ i < n) and decrease the temperature of the CPU cores 0, 1, ... i by 1.
  • Select a position i (0 ≤ i < n) and decrease the temperature of the CPU cores i, i+1, ... n -1 by 1.
  • Increase the temperature of all cores by 1.
  • Formally, given an array temperature of size n, the initial temperature of each core, find the minimum number of actions required for the Optimizer, to make the temperature of each core equal to 0.

    Function

    regulateTemperatures(temperature: int[]) → long

    Complete the function regulateTemperatures in the editor below.

    regulateTemperatures has the following parameter:

    1. int temperature[n]: initial temperature of each core

    Returns

    long: the minimum number of actions required to make the temperature of each core equal to 0.

    Examples

    Example 1

    temperature = [2, 4, 4]return = 4

    One of the optimal ways to make the temperature of all cores 0 is as follows:

    1. Select index 1 and use the ability of type 2. The new temperatures will be [2, 3, 3].
    2. Select index 2 and use the ability of type 1. The new temperatures will be [1, 2, 2].
    3. Select index 1 and use the ability of type 2. The new temperatures will be [1, 1, 1].
    4. Select index 2 and use the ability of type 1. The new temperatures will be [0, 0, 0].

    It can be shown that it is not possible to make the temperature of each core equal to 0 in less than 4 actions, hence, the answer is 4.

    Example 2

    temperature = [2, -2, -3, 1]return = 10
    One of the optimal ways to make the temperature of all cores 0 is as follows: Select index 3 and use the ability of type 2. The new temperatures will be [2, -2, -3, -2]. Select index 1 and use the ability of type 1. The new temperatures will be [1, -3, -3, -21. Use the ability of type 3. The new temperatures will be [2, -2, -2, -1]: Select index 3 and use the ability of type 2. The new temperatures will be [2, -2, -2, -2]. Select index 0 and use the ability of type 1. The new temperatures will be [1, -2, -2, -2]. Select index 0 and use the ability of type 1. The new temperatures will be [O, -2, -2, -2]. Use the ability of typé 3. The new temperatures will be [1, -1, -1, -1]. Use the ability of type 3. The new temperatures will be [2, 0, 0, 0]. Select index 0 and use the ability of type 1. The new temperatures will be [1, 0, 0, 0]. It can be shown that it is not possible to make the temperature of each core equal to 0 in less than 10 actions, hence, the answer is 10.

    Constraints

    🍊🍊

    More Amazon problems

    drafts saved locally
    public long regulateTemperatures(int[] temperature) {
      // write your code here
    }
    
    temperature[2, 4, 4]
    expected4
    checking account