将循环数组的所有元素减少到 0 所需的最小减量数
给定一个由N个整数组成的循环数组arr[] ,任务是找到将循环数组的所有元素减少到0的最小操作数。在每个操作中,将当前元素减1 (从第一个元素开始)并移动到下一个元素。
例子:
Input: arr[] = {2, 0, 2}
Output: 6
Explanation:
Following are the operations performed:
- Reduce the array element arr[0] by 1 modifies the array to {1, 0, 2} and move to the next element arr[1].
- Do nothing and move to the next element i.e., arr[2].
- Reduce the array element arr[2] by 1 modifies the array to {1, 0, 1} and move to the next element arr[0].
- Reduce the array element arr[0] by 1 modifies the array to {0, 0, 1} and move to the next element arr[1].
- Do nothing and move to the next element i.e., arr[2].
- Reduce the array element arr[2] by 1 modifies the array to {0, 0, 0} and move to the next element arr[0].
After the above operations, all the array elements of the circular array has been reduced to 0. Therefore, the minimum number of operations required is 6.
Input: arr[] = {0, 3, 1, 3, 2}
Output: 14
朴素方法:解决给定问题的最简单方法是通过执行给定操作以循环顺序遍历给定数组,直到所有数组元素为0 ,并跟踪执行的操作计数。完成上述步骤后,打印执行的操作次数。
时间复杂度: O(N*M),其中M是数组的最大元素。
辅助空间: O(1)
高效的方法:上述方法也可以通过找到最大数组元素的最后一个索引并找到相应的最小操作次数来优化。请按照以下步骤解决问题:
- 初始化两个变量,分别存储最大元素和最大元素的最后一个索引的pos和M。
- 使用变量i遍历给定数组,如果arr[i]的值至少为M ,则将M的值修改为arr[i] ,将pos修改为i 。
- 完成上述步骤后,打印(mx – 1)*N + pos +1的值作为结果的最小步数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum operation
// require to make all elements 0
void minimumOperations(int arr[], int N)
{
// Stores the maximum element and
// its position in the array
int mx = 0, pos = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update the maximum element
// and its index
if (arr[i] >= mx) {
mx = arr[i];
pos = i;
}
}
// Print the minimum number of
// operations required
cout << (mx - 1) * N + pos + 1;
}
// Driver Code
int main()
{
int arr[] = { 2, 0, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
minimumOperations(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find minimum operation
// require to make all elements 0
static void minimumOperations(int arr[], int N)
{
// Stores the maximum element and
// its position in the array
int mx = 0, pos = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update the maximum element
// and its index
if (arr[i] >= mx) {
mx = arr[i];
pos = i;
}
}
// Print the minimum number of
// operations required
System.out.println((mx - 1) * N + pos + 1);
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 2, 0, 2 };
int N = arr.length;
minimumOperations(arr, N);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program for the above approach
# Function to find minimum operation
# require to make all elements 0
def minimumOperations(arr, N):
# Stores the maximum element and
# its position in the array
mx = 0
pos = 0
# Traverse the array
for i in range(N):
# Update the maximum element
# and its index
if (arr[i] >= mx):
mx = arr[i]
pos = i
# Print the minimum number of
# operations required
print((mx - 1) * N + pos + 1)
# Driver Code
if __name__ == '__main__':
arr = [ 2, 0, 2 ]
N = len(arr)
minimumOperations(arr, N)
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program for the above approach
using System;
class GFG{
// Function to find minimum operation
// require to make all elements 0
static void minimumOperations(int[] arr, int N)
{
// Stores the maximum element and
// its position in the array
int mx = 0, pos = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update the maximum element
// and its index
if (arr[i] >= mx) {
mx = arr[i];
pos = i;
}
}
// Print the minimum number of
// operations required
Console.Write((mx - 1) * N + pos + 1);
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 0, 2 };
int N = arr.Length;
minimumOperations(arr, N);
}
}
// This code is contributed by splevel62.
Javascript
输出:
6
时间复杂度: O(N)
辅助空间: O(1)