📌  相关文章
📜  增加和减少Array达到0或N的最小步骤

📅  最后修改于: 2021-05-07 01:18:37             🧑  作者: Mango

给定一个整数N和两个数组递增[]递减[] ,因此它们只有1到N个元素。任务是找到两个数组中每个元素达到0N的最小步骤数。步骤定义如下:

  • 只需一步, incrementing []数组的所有元素都会增加1,而reductioning []数组的所有元素都会减少1。
  • 当元素变为0N时,将不再对其执行增减操作。

例子:

方法:想法是在递增[]数组的所有元素所需的步骤之间找到最大值 ,而减少的[]数组分别达到N0 。步骤如下:

  1. 求出递增[]数组的最小元素。
  2. N – min(increasing [])给出了递增[]数组的所有元素达到N的最大步长。
  3. 找到数组的最大元素减少[]
  4. max [decreasing [])给出了减少[]数组的所有元素达到0的最大步长。
  5. 因此,当所有元素变为0N时的最小步数由max(N – min(increasing []),max(decreasing []))给出

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that finds the minimum
// steps to reach either 0 or N for
// given increasing and decreasing
// arrays
void minSteps(int N, int increasing[],
              int decreasing[], int m1, int m2)
{
 
    // Initialize variable to
    // find the minimum element
    int mini = INT_MAX;
 
    // Find minimum element in
    // increasing[] array
    for(int i = 0; i < m1; i++)
    {
        if (mini > increasing[i])
            mini = increasing[i];
    }
 
    // Initialize variable to
    // find the maximum element
    int maxi = INT_MIN;
 
    // Find maximum element in
    // decreasing[] array
    for(int i = 0; i < m2; i++)
    {
        if (maxi < decreasing[i])
            maxi = decreasing[i];
    }
 
    // Find the minimum steps
    int minSteps = max(maxi,
                       N - mini);
 
    // Print the minimum steps
    cout << minSteps << endl;
}
 
// Driver code
int main()
{
     
    // Given N
    int N = 7;
 
    // Given increasing
    // and decreasing array
    int increasing[] = { 3, 5 };
    int decreasing[] = { 6 };
     
    // Find length of arrays
    // increasing and decreasing
    int m1 = sizeof(increasing) /sizeof(increasing[0]);
    int m2 = sizeof(decreasing) / sizeof(decreasing[0]);
     
    // Function call
    minSteps(N, increasing, decreasing, m1, m2);
}
 
// This code is contributed by Manne Sree Charan


Java
// Java program for the above approach
import java.util.*;
 
public class GFG {
 
    // Function that finds the minimum
    // steps to reach either 0 or N for
    // given increasing and decreasing
    // arrays
    public static void
    minSteps(int N, int[] increasing,
            int[] decreasing)
    {
 
        // Initialize variable to
        // find the minimum element
        int min = Integer.MAX_VALUE;
 
        // Find minimum element in
        // increasing[] array
        for (int i : increasing) {
            if (min > i)
                min = i;
        }
 
        // Initialize variable to
        // find the maximum element
        int max = Integer.MIN_VALUE;
 
        // Find maximum element in
        // decreasing[] array
        for (int i : decreasing) {
            if (max < i)
                max = i;
        }
 
        // Find the minimum steps
        int minSteps = Math.max(max,
                                N - min);
 
        // Print the minimum steps
        System.out.println(minSteps);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given N
        int N = 7;
 
        // Given increasing
        // and decreasing array
        int increasing[] = { 3, 5 };
        int decreasing[] = { 6 };
 
        // Function call
        minSteps(N, increasing, decreasing);
    }
}


Python3
# Python3 program for
# the above approach
import sys
 
# Function that finds the minimum
# steps to reach either 0 or N for
# given increasing and decreasing
# arrays
def minSteps(N, increasing, decreasing):
    # Initialize variable to
    # find the minimum element
    Min = sys.maxsize;
 
    # Find minimum element in
    # increasing array
    for i in increasing:
        if (Min > i):
            Min = i;
 
    # Initialize variable to
    # find the maximum element
    Max = -sys.maxsize;
 
    # Find maximum element in
    # decreasing array
    for i in decreasing:
        if (Max < i):
            Max = i;
 
    # Find the minimum steps
    minSteps = max(Max, N - Min);
 
    # Prthe minimum steps
    print(minSteps);
 
# Driver Code
if __name__ == '__main__':
   
    # Given N
    N = 7;
 
    # Given increasing
    # and decreasing array
    increasing = [3, 5];
    decreasing = [6];
 
    # Function call
    minSteps(N, increasing, decreasing);
 
# This code contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function that finds the minimum
// steps to reach either 0 or N for
// given increasing and decreasing
// arrays
public static void minSteps(int N, int[] increasing,
                                   int[] decreasing)
{
 
    // Initialize variable to
    // find the minimum element
    int min = int.MaxValue;
 
    // Find minimum element in
    // increasing[] array
    foreach(int i in increasing)
    {
        if (min > i)
            min = i;
    }
 
    // Initialize variable to
    // find the maximum element
    int max = int.MinValue;
 
    // Find maximum element in
    // decreasing[] array
    foreach(int i in decreasing)
    {
        if (max < i)
            max = i;
    }
 
    // Find the minimum steps
    int minSteps = Math.Max(max,
                            N - min);
 
    // Print the minimum steps
    Console.WriteLine(minSteps);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given N
    int N = 7;
 
    // Given increasing
    // and decreasing array
    int []increasing = { 3, 5 };
    int []decreasing = { 6 };
 
    // Function call
    minSteps(N, increasing, decreasing);
}
}
 
// This code is contributed by Amit Katiyar


输出:
6


时间复杂度: O(N)
辅助空间: O(1)