📌  相关文章
📜  将循环数组的所有元素减少到 0 所需的最小减量数

📅  最后修改于: 2022-05-13 01:56:08.327000             🧑  作者: Mango

将循环数组的所有元素减少到 0 所需的最小减量数

给定一个由N个整数组成的循环数组arr[] ,任务是找到将循环数组的所有元素减少到0的最小操作数。在每个操作中,将当前元素减1从第一个元素开始)并移动到下一个元素。

例子:

朴素方法:解决给定问题的最简单方法是通过执行给定操作以循环顺序遍历给定数组,直到所有数组元素为0 ,并跟踪执行的操作计数。完成上述步骤后,打印执行的操作次数。

时间复杂度: O(N*M),其中M数组的最大元素
辅助空间: O(1)

高效的方法:上述方法也可以通过找到最大数组元素的最后一个索引并找到相应的最小操作次数来优化。请按照以下步骤解决问题:

  • 初始化两个变量,分别存储最大元素和最大元素的最后一个索引的posM。
  • 使用变量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)