📌  相关文章
📜  使数组的所有元素相等的最小步骤数

📅  最后修改于: 2021-04-21 21:57:30             🧑  作者: Mango

给定两个相同长度的数组A []B [] ,任务是通过用与数组B相对应的元素之差替换元素,找到使数组的所有元素相等所需的最小步数。
注意:如果无法打印,请打印-1。
例子:

方法:想法是找到可以收敛数组元素的值,该值肯定在0到数组的最小值之间。下面是该方法的说明:

  • 最初,将标志标记为False,以检查该数组是否可转换。
  • 运行while循环,直到数组的最小值大于-1。
    • 遍历数组的索引,即从0到长度– 1
    • 对于每个索引,检查数组A中该索引处的元素是否大于数组的最小值;如果是,则将数组元素A [i]更新为A [i] – B [i]。
    • 将步数增加1。
    • 如果是,请检查所有数组元素是否相等,然后将标志标记为True并中断while循环
    • 否则,请重复上述步骤以计算所需的步骤总数。
  • 最后,检查该标志是否为True,如果是,则该数组是可转换的。

举例说明:
给定数组为– A [] = {5,7,10,5,15},B [] = {2,2,1,3,5}

Array A Current Index Minimum of Array A Steps Comments
{5, 7, 10, 5, 15} 0 5 0 No operation !
{5, 5, 10, 5, 15} 1 5 1 Array element is subtracted once. 7 – 2 = 5
{5, 5, 9, 5, 15} 2 5 2 Array element is subtracted once. 10 – 1 = 9
{5, 5, 8, 5, 15} 2 5 3 Array element is subtracted once. 9 – 1 = 8
{5, 5, 7, 5, 15} 2 5 4 Array element is subtracted once. 8 – 1 = 7
{5, 5, 6, 5, 15} 2 5 5 Array element is subtracted once. 7 – 1 = 6
{5, 5, 5, 5, 15} 2 5 6 Array element is subtracted once. 6 – 1 = 5
{5, 5, 5, 5, 10} 4 5 7 Array element is subtracted once. 15 – 5 = 10
{5, 5, 5, 5, 5} 4 5 8 Array element is subtracted once. 10 – 5 = 5

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum number of steps to convert
// array by subtracting the corresponding
// element from array B
#include 
using namespace std;
  
// Function to find the minimum steps
void minimumSteps(int ar1[], int ar2[], int n)
{
     
    // Counter to store the steps
    int ans = 0;
      
    // Flag to check that
    // array is converted
    bool flag = true;
      
    // Loop until the minimum of the
    // array is greater than -1
    while (*min_element(ar1, ar1 + n) > -1)
    {
        int a = *min_element(ar1, ar1 + n);
         
        // Loop to convert the array
        // elements by substraction
        for(int i = 0; i < n; i++)
        {
            if (ar1[i] != a)
            {
                ar1[i] -= ar2[i];
                ans += 1;
            }
        }
         
        set s1(ar1, ar1 + n);
         
        // If the array is converted
        if (s1.size() == 1)
        {
            flag = false;
            cout << ans << endl;
            break;
        }
    }
     
    if (flag)
    {
        cout << -1 << endl;
    }
}
         
// Driver Code
int main()
{
    int n = 5;
    int ar1[] = { 5, 7, 10, 5, 15 };
    int ar2[] = { 1, 2, 1, 5, 5 };
      
    // Function call
    minimumSteps(ar1, ar2, n);
  
    return 0;
}
 
// This code is contributed by rutvik_56


Java
// Java implementation to find the
// minimum number of steps to convert
// array by subtracting the corresponding
// element from array B
import java.util.*;
class GFG{
  
// Function to find the
// minimum steps
static void minimumSteps(int ar1[],
                         int ar2[],
                         int n)
{
  // Counter to store the steps
  int ans = 0;
 
  // Flag to check that
  // array is converted
  boolean flag = true;
 
  // Loop until the minimum of the
  // array is greater than -1
  while (Arrays.stream(ar1).min().getAsInt() > -1)
  {
    int a = Arrays.stream(ar1).min().getAsInt();
 
    // Loop to convert the array
    // elements by substraction
    for(int i = 0; i < n; i++)
    {
      if (ar1[i] != a)
      {
        ar1[i] -= ar2[i];
        ans += 1;
      }
    }
 
    HashSet s1 = new HashSet<>();
    for(int i = 0; i < n; i++)
    {
      s1.add(ar1[i]);
    }
 
    // If the array is converted
    if (s1.size() == 1)
    {
      flag = false;
      System.out.print(ans + "\n");
      break;
    }
  }
 
  if (flag)
  {
    System.out.print(-1 + "\n");
  }
}
         
// Driver Code
public static void main(String[] args)
{
  int n = 5;
  int ar1[] = {5, 7, 10, 5, 15};
  int ar2[] = {1, 2, 1, 5, 5};
 
  // Function call
  minimumSteps(ar1, ar2, n);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation to find the
# minimum number of steps to convert
# array by subtracting the corresponding
# element from array B
 
# Function to find the minimum steps
def minimumSteps(ar1, ar2, n):
     
    # Counter to store the steps
    ans = 0
     
    # Flag to check that
    # array is converted
    flag = True
     
    # Loop until the minimum of the
    # array is greater than -1
    while min(ar1)>-1:
        a = min(ar1)
         
        # Loop to convert the array
        # elements by substraction
        for i in range(n):
            if ar1[i]!= a:
                ar1[i]-= ar2[i]
                ans+= 1
                 
        # If the array is converted
        if len(set(ar1))== 1:
            flag = False
            print(ans)
            break
    if flag:
        print(-1)
 
# Driver Code
if __name__ == "__main__":
    n = 5
    ar1 = [5, 7, 10, 5, 15]
    ar2 = [1, 2, 1, 5, 5]
     
    # Function Call
    minimumSteps(ar1, ar2, n)


C#
// C# implementation to
// find the minimum number
// of steps to convert array
// by subtracting the
// corresponding element from
// array B
using System;
using System.Linq;
using System.Collections.Generic;
class GFG{
  
// Function to find the
// minimum steps
static void minimumSteps(int []ar1,
                         int []ar2,
                         int n)
{
  // Counter to store the steps
  int ans = 0;
 
  // Flag to check that
  // array is converted
  bool flag = true;
 
  // Loop until the minimum of the
  // array is greater than -1
  while (ar1.Min() > -1)
  {
    int a = ar1.Min();
 
    // Loop to convert the array
    // elements by substraction
    for(int i = 0; i < n; i++)
    {
      if (ar1[i] != a)
      {
        ar1[i] -= ar2[i];
        ans += 1;
      }
    }
 
    HashSet s1 = new HashSet();
     
    for(int i = 0; i < n; i++)
    {
      s1.Add(ar1[i]);
    }
 
    // If the array is converted
    if (s1.Count == 1)
    {
      flag = false;
      Console.Write(ans + "\n");
      break;
    }
  }
 
  if (flag)
  {
    Console.Write(-1 + "\n");
  }
}
         
// Driver Code
public static void Main(String[] args)
{
  int n = 5;
  int []ar1 = {5, 7, 10, 5, 15};
  int []ar2 = {1, 2, 1, 5, 5};
 
  // Function call
  minimumSteps(ar1, ar2, n);
}
}
 
// This code is contributed by 29AjayKumar


输出:
8





性能分析:

  • 时间复杂度:与上述方法一样,在最坏的情况下,有两个循环来转换占用O(N 2 )的数组,因此时间复杂度将为O(N 2 )
  • 空间复杂度:与上述方法一样,没有使用额外的空间,因此空间复杂度将为O(1)