FastPrepFastPrep
Problem Brief

Regulate Temperatures

NEW GRADOA

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 Description

    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.

    1Example 1

    Input
    temperature = [2, 4, 4]
    Output
    4
    Explanation

    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.

    2Example 2

    Input
    temperature = [2, -2, -3, 1]
    Output
    10
    Explanation
    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

    Limits and guarantees your solution can rely on.

    🍊🍊
    public long regulateTemperatures(int[] temperature) {
      // write your code here
    }
    
    Input

    temperature

    [2, 4, 4]

    Output

    4

    Sign in to submit your solution.