给定一个由N个元素组成的数组,其中第一个元素为非零正数M ,其余N – 1个元素为0,任务是计算使整个数组相等的最小步数,同时遵守以下规定规则:
1.仅当第i-1个元素严格大于第i个元素时,第i个元素才能增加1
2.如果将第i个元素增加1,则无法同时增加第i + 1个元素。(即,不能同时增加连续的元素)
3.多个元素可以在一个步骤中同时增加。
例子:
Input : N = 3, M = 4
Output : 8
Explanation:
array is 4 0 0
In 4 steps element at index 1 is increased, so the array becomes {4, 4, 0}. In the next 4 steps the element at index 3 is increased so array becomes {4, 4, 4}
Thus, 4 + 4 = 8 operations are required to make all the array elements equal
Input : N = 4, M = 4
Output : 9
Explanation:
The steps are shown in the flowchart given below
Refer to the flowchart given below.
方法:
为了最大程度地增加每步增量,将创建更多数量的不平衡(array [i]> array [i + 1]),
步骤1 ,元素0>元素1,因此元素1递增,
步骤2 ,元素1>元素2,因此元素2增加1
步骤3 ,元素0>元素1和元素2>元素3,因此元素1&3递增1
步骤4 ,元素1>元素2元素3>元素4,因此元素2和4递增
步骤5 ,元素0>元素1;元素2>元素3;元素4>元素5;因此元素1、3,&5递增。
等等…
考虑以下数组,
5 0 0 0 0 0
1) 5 1 0 0 0 0
2)5 1 1 0 0 0
3) 5 2 1 1 0 0
4)5 2 2 1 1 0
5) 5 3 2 2 1 1
6)5 3 3 2 2 1
7) 5 4 3 3 2 2
8)5 4 4 3 3 2
9) 5 5 4 4 3 3
10)5 5 5 4 4 3
11)5 5 5 5 4 4
12)5 5 5 5 5 4
13)5 5 5 5 5 5
请注意,在创建不平衡之后(即array [i]> array [i + 1]),该元素在其他步骤中递增1。在步骤1中,元素1递增为1,在步骤2中元素2递增为1,在步骤3中元素3递增为1,因此在步骤n-1中,第n-1个元素将变为1。第1个元素在其他步骤上增加1,直到达到元素0的值。然后整个数组变得相等。
所以最后一个元素后面的模式是
(0,0,0 ..,0)直到第(N – 4)个元素变为1,即n-4步
在那之后,
(0,0,1,1,2,2,2,3,4,4,…M – 1,M – 1,M)是2 * m +1步。
因此,最终结果为(N – 3)+ 2 * M
有一些极端的情况需要处理,即。当N = 1时,数组只有一个元素,因此所需步骤数=0。当N = 2时,所需步骤数等于M
C++
// C++ program to make the array elements equal in minimum steps
#include
using namespace std;
// Returns the minumum steps required to make an array of N
// elements equal, where the first array element equals M
int steps(int N, int M)
{
// Corner Case 1: When N = 1
if (N == 1)
return 0;
// Corner Case 2: When N = 2
else if (N == 2) // corner case 2
return M;
return 2 * M + (N - 3);
}
// Driver Code
int main()
{
int N = 4, M = 4;
cout << steps(N, M);
return 0;
}
Java
// Java program to make the array elements
// equal in minimum steps
import java.io.*;
class GFG {
// Returns the minumum steps required
// to make an array of N elements equal,
// where the first array element equals M
static int steps(int N, int M)
{
// Corner Case 1: When N = 1
if (N == 1)
return 0;
// Corner Case 2: When N = 2
else if (N == 2) // corner case 2
return M;
return 2 * M + (N - 3);
}
// Driver Code
public static void main (String[] args)
{
int N = 4, M = 4;
System.out.print( steps(N, M));
}
}
// This code is contributed by anuj_67.
Python3
# Python program to make
# the array elements equal
# in minimum steps
# Returns the minumum steps
# required to make an array
# of N elements equal, where
# the first array element
# equals M
def steps(N, M):
# Corner Case 1: When N = 1
if (N == 1):
return 0
# Corner Case 2: When N = 2
elif(N == 2):
return M
return 2 * M + (N - 3)
# Driver Code
N = 4
M = 4
print(steps(N,M))
# This code is contributed
# by Shivi_Aggarwal.
C#
// C# program to make the array
// elements equal in minimum steps
using System;
class GFG
{
// Returns the minumum steps
// required to make an array
// of N elements equal, where
// the first array element
// equals M
static int steps(int N, int M)
{
// Corner Case 1: When N = 1
if (N == 1)
return 0;
// Corner Case 2: When N = 2
else if (N == 2) // corner case 2
return M;
return 2 * M + (N - 3);
}
// Driver Code
public static void Main ()
{
int N = 4, M = 4;
Console.WriteLine(steps(N, M));
}
}
// This code is contributed by anuj_67.
PHP
9