A risk modeling system uses a scaling computing system that implements an autoscale policy depending on the current load or utilization of the computing system.
The system starts with a number of computing instances given by instances. The system polls the instances every second to see the average utilization at that second, and performs scaling as described below. Once any action is taken, the system will stop polling for 10 seconds. During that time, the number of instances does not change.
Average utilization > 60%: Double the number of instances if the doubled value does not exceed 2 * 10^8. This is an action. If the number of instances exceeds this limit on doubling, perform no action.
Average utilization < 25%: Halve the number of instances if the number of instances is greater than 1 (take ceil if the number is not an integer). This is also an action. If the number of instances is 1, take no action.
25% <= Average utilization <= 60%: No action.
Given an array of the values of the average utilization at each second for this system, determine the number of instances at the end of the time frame.
For example, the system starts with instances = 2, and average utilization is given as averageUtil = [25, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 76, 80].
At the first second, utilization is 25, so no action is taken.
At the second second, averageUtil[1] = 23 < 25, so instances = 2 / 2 = 1. The next 10 seconds, averageUtil[2]..averageUtil[11], no polling is done.
At averageUtil[11] = 76, 76 > 60 so the number of instances is doubled. There are no more readings to consider and 2 is the final answer.
Constraints:
1 <= instances <= 10^5
1 <= n <= 10^5
1 <= averageUtil[i] <= 100
Example 1:
Input: averageUtil=[5, 10, 80], instances = 1
Output: 2
Explanation:
Here instance = 1 and averageUtil = [5, 10, 80]. At the 1st and 2nd seconds of the time period, no action will be taken because the utilization is less than 25%, the number of instance is 1. At the 3rd second, the number of instances will double to 2.
Example 2:
Input: averageUtil=[30, 5, 4, 8, 19, 89], instances = 5
Output: 3
Explanation:
At second 1,25 <= averageUtil[0] = 30 <= 60, so take no action.
At second 2, averageUtil[1] = 5<25, so an action is instantiated to halve the number of instances to ceil(5/2) = 3.
The system will stop checking for 10 seconds, so from averageUtil[2] through averageUtil[5] no actions will be taken.
There are no more readings to consider and 3 is the final answer.
Copyright © A++ Code Bootcamp 2023