通过循环移动段或用最大值替换前缀来最小化使所有 Array 元素相等的步骤
给定一个由N个正整数组成的数组arr[] ,任务是通过对数组执行任意次数(可能为 0)的以下操作,打印使数组中所有元素相等所需的最小步数。
- 操作 1:选择任何前缀arr[1…k]使得 max (arr[1], arr[2], …, arr[k]) = arr[k] 并设置arr[i] = arr[k]对于[1,k−1]范围内的所有i 。
- 操作2:选择任何段[L,R]并将该段循环向左移动,保持其他元素不变。
例子:
Input: arr[] = {1, 2, 12, 20, 18, 19}
Output: 2
Explanation: It can be solved in two steps:
In the first step, apply operation 2 by choosing L = 4 and R = 6 yields the sequence {1, 2, 12, 18, 19, 20}.
In the second step, apply operation 1 on the prefix arr{1, 2, 12, 18, 19, 20} which converts arr into {20, 20, 20, 20, 20} where all elements are equal.
Input: arr[] = {2, 2, 2, 2}
Output: 0
Explanation: the elements in the array are all same.
方法:给定的问题可以通过使用以下观察来解决:
- 如果所有元素都相同,则不需要进行任何操作。
- 如果数组的最大值最后是,那么只对整个数组执行操作 1 会使所有元素相等。
- 如果最大值在最后一个位置之前的某个位置,则说 j。然后在段 [j, N] 中执行操作 2,然后对前缀 [1, N] 执行操作 1,使所有元素相等。
因此,根据上述观察,答案可以是 0、1 或 2。请按照下面提到的步骤进行操作。
- 迭代数组并从数组中找到最大值。
- 如果数组的所有元素都相同,则返回 0。
- 如果最大值出现在数组的末尾,则答案为 1,如观察结果所示。
- 如果这两个都不是,那么答案是 2。
下面是上述方法的实现:
C++
// C++ code to implement the above approach
#include
using namespace std;
// Function to print the minimum number
// of steps required to make all
// the elements are equal
void solve(int arr[], int N)
{
// Initially assume all elements
// are equal
bool isEqual = true;
// For finding the largest element
int maxi = arr[0];
// Loop to iterate through the array
for (int i = 1; i < N; i++) {
// If any element is not equal
// than previous element, mark
// it as false
if (arr[i] != arr[i - 1]) {
isEqual = false;
}
// Storing greater element
maxi = max(maxi, arr[i]);
}
// If all the elements are equal
if (isEqual) {
cout << "0";
}
// If max element is found
// at the last index
else if (maxi == arr[N - 1]) {
cout << "1";
}
// If max element is found
// other than the last index
else {
cout << "2";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 12, 20, 18, 19 };
int N = sizeof(arr) / sizeof(arr[0]);
solve(arr, N);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to print the minimum number
// of steps required to make all
// the elements are equal
static void solve(int arr[], int N)
{
// Initially assume all elements
// are equal
boolean isEqual = true;
// For finding the largest element
int maxi = arr[0];
// Loop to iterate through the array
for (int i = 1; i < N; i++) {
// If any element is not equal
// than previous element, mark
// it as false
if (arr[i] != arr[i - 1]) {
isEqual = false;
}
// Storing greater element
maxi = Math.max(maxi, arr[i]);
}
// If all the elements are equal
if (isEqual) {
System.out.println("0");
}
// If max element is found
// at the last index
else if (maxi == arr[N - 1]) {
System.out.println("1");
}
// If max element is found
// other than the last index
else {
System.out.println("2");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 12, 20, 18, 19 };
int N = arr.length;
solve(arr, N);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 code to implement the above approach
# Function to print the minimum number
# of steps required to make all
# the elements are equal
def solve(arr, N):
# Initially assume all elements
# are equal
isEqual = True
# For finding the largest element
maxi = arr[0]
# Loop to iterate through the array
for i in range(1, N):
# If any element is not equal
# than previous element, mark
# it as false
if (arr[i] != arr[i - 1]):
isEqual = False
# Storing greater element
maxi = max(maxi, arr[i])
# If all the elements are equal
if (isEqual):
print("0")
# If max element is found
# at the last index
elif (maxi == arr[N - 1]):
print("1")
# If max element is found
# other than the last index
else:
print("2")
# Driver Code
if __name__=="__main__":
arr = [ 1, 2, 12, 20, 18, 19 ]
N = len(arr)
solve(arr, N)
# This code is contributed by Akash Jha
C#
// C# code to implement the above approach
using System;
class GFG {
// Function to print the minimum number
// of steps required to make all
// the elements are equal
static void solve(int[] arr, int N)
{
// Initially assume all elements
// are equal
bool isEqual = true;
// For finding the largest element
int maxi = arr[0];
// Loop to iterate through the array
for (int i = 1; i < N; i++) {
// If any element is not equal
// than previous element, mark
// it as false
if (arr[i] != arr[i - 1]) {
isEqual = false;
}
// Storing greater element
maxi = Math.Max(maxi, arr[i]);
}
// If all the elements are equal
if (isEqual) {
Console.WriteLine("0");
}
// If max element is found
// at the last index
else if (maxi == arr[N - 1]) {
Console.WriteLine("1");
}
// If max element is found
// other than the last index
else {
Console.WriteLine("2");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 12, 20, 18, 19 };
int N = arr.Length;
solve(arr, N);
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(1)