Regulate Temperature

Regulate Temperature

There is a system equipped with n CPU cores, each with a specific temperature denoted by temperature[i].

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:

  • 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.




Get one-to-one training from Google Facebook engineers

Top-notch Professionals

Learn from Facebook and Google senior engineers interviewed 100+ candidates.
Most recent interview questions and system design topics gathered from aonecode alumnus.
One-to-one online classes. Get feedbacks from real interviewers.

Customized Private Class

Already a coding expert? - Advance straight to hard interview topics of your interest.
New to the ground? - Develop basic coding skills with your own designated mentor.
Days before interview? - Focus on most important problems in target company question bank.