Regulate Temperatures
Learn this problemProblem 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:
i (0 ≤ i < n) and decrease the temperature of the CPU cores 0, 1, ... i by 1.i (0 ≤ i < n) and decrease the temperature of the CPU cores i, i+1, ... n -1 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:
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 = 4One of the optimal ways to make the temperature of all cores 0 is as follows:
- Select index 1 and use the ability of type 2. The new temperatures will be [2, 3, 3].
- Select index 2 and use the ability of type 1. The new temperatures will be [1, 2, 2].
- Select index 1 and use the ability of type 2. The new temperatures will be [1, 1, 1].
- 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 = 10Constraints
🍊🍊More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026