自动缩放后在时间范围结束时查找实例
给定一个整数、 instances和一个大小为N的数组arr[] ,表示每秒计算系统的平均利用率百分比,任务是找到时间帧结束时的实例数,使得计算系统根据以下规则自动缩放实例数:
- Average utilization < 25%: Reduce the number of instances by half if the number of instances is greater than 1.
- 25% ≤ Average utilization ≤ 60%: Take no action.
- Average utilization > 60%: Double the number of instances if the doubled value does not exceed 2* 108.
Once an action of adding or reducing the number of instances is performed, the system will stop monitoring for 10 seconds. During that time, the number of instances does not change.
例子:
Input: instances = 2, arr[] = {25, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 76, 80}
Output: 2
Explanation:
At second 1, arr[0] = 25 ≤ 25, so take no action.
At second 2, arr[1] = 23 < 25, so an action is instantiated to halve the number of instances to ceil(2/2) = 1. The system will stop checking for 10 seconds, so from arr[2] through arr[11] no actions will be taken.
At second 13, arr[12] = 76 > 60, so the number of instances is doubled from 1 to 2.
There are no more readings to consider and 2 is the final value.
Input: instances = 5, arr = {30, 5, 4, 8, 19, 89}
Output: 3
Explanation:
At second 1, 25 ≤ arr[0] = 30 ≤ 60, so take no action.
At second 2, arr[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 arr[2] through arr[5] no actions will be taken.
There are no more readings to consider and 3 is the final answer.
方法:可以通过遍历给定数组arr[]来解决给定的问题,如果当前元素小于 25,则如果实例数大于 1,则将实例数除以 2。否则,如果当前值为大于 60 如果实例数不大于 10 8 ,则将实例数乘以 2 ,在执行这两个操作中的任何一个后,将当前索引增加 10。按照以下步骤解决问题:
- 使用变量i遍历数组arr[]并执行以下步骤:
- 如果arr[i]小于 25 并且实例大于 1,则将实例除以 2 并将i增加10 。
- 如果arr[i]大于 60 并且实例小于或等于 10 8 ,则将实例乘以 2 并将i增加10 。
- 将索引i增加1 。
- 完成上述步骤后,打印实例数作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of
// instances after compintion
void finalInstances(int instances,
int arr[], int N)
{
int i = 0;
// Traverse the array, arr[]
while (i < N)
{
// If current element is less
// than 25
if (arr[i] < 25 && instances > 1)
{
// Divide instances by 2 and take ceil value
double temp = (instances / 2.0);
instances = (int)(ceil(temp));
i = i + 10;
}
// If the current element is
// greater than 60
else if (arr[i] > 60 &&
instances <= (2*pow(10, 8)))
{
// Double the instances
instances = instances * 2;
i = i + 10;
}
i = i + 1;
}
// Print the instances at the end
// of the traversal
cout << instances;
}
// Driver Code
int main()
{
int instances = 2;
int arr[] = { 25, 23, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 76, 80 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
finalInstances(instances, arr, N);
}
// This code is contributed by splevel62
Java
// Java program for above approach
class GFG{
// Function to find the number of
// instances after compintion
public static void finalInstances(int instances,
int[] arr)
{
int i = 0;
// Traverse the array, arr[]
while (i < arr.length)
{
// If current element is less
// than 25
if (arr[i] < 25 && instances > 1)
{
// Divide instances by 2
instances = (instances / 2);
i = i + 10;
}
// If the current element is
// greater than 60
else if (arr[i] > 60 &&
instances <= Math.pow(10, 8))
{
// Double the instances
instances = instances * 2;
i = i + 10;
}
i = i + 1;
}
// Print the instances at the end
// of the traversal
System.out.println(instances);
}
// Driver Code
public static void main(String args[])
{
int instances = 2;
int[] arr = { 25, 23, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 76, 80 };
// Function Call
finalInstances(instances, arr);
}
}
// This code is contributed by _saurabh_jaiswal
Python3
# Python program for the above approach
from math import ceil
# Function to find the number of
# instances after completion
def finalInstances(instances, arr):
i = 0
# Traverse the array, arr[]
while i < len(arr):
# If current element is less
# than 25
if arr[i] < 25 and instances > 1:
# Divide instances by 2
instances = ceil(instances / 2)
i += 10
# If the current element is
# greater than 60
elif arr[i] > 60 and instances <= 10**8:
# Double the instances
instances *= 2
i += 10
i += 1
# Print the instances at the end
# of the traversal
print(instances)
# Driver Code
instances = 2
arr = [25, 23, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 76, 80]
# Function Call
finalInstances(instances, arr)
C#
// C# program for above approach
using System;
class GFG{
// Function to find the number of
// instances after compintion
static void finalInstances(int instances, int[] arr)
{
int i = 0;
// Traverse the array, arr[]
while (i < arr.Length) {
// If current element is less
// than 25
if (arr[i] < 25 && instances > 1) {
// Divide instances by 2
instances = (instances / 2);
i = i + 10;
}
// If the current element is
// greater than 60
else if (arr[i] > 60 && instances <= Math.Pow(10, 8))
{
// Double the instances
instances = instances * 2;
i = i + 10;
}
i = i + 1;
}
// Print the instances at the end
// of the traversal
Console.Write(instances);
}
// Driver Code
static void Main()
{
int instances = 2;
int[] arr = {25, 23, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 76, 80};
// Function Call
finalInstances(instances, arr);
}
}
// This code is contributed by sanjoy_62.
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)