📌  相关文章
📜  使用给定操作使数组不递减

📅  最后修改于: 2021-04-27 18:54:10             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是检查是否有可能通过在每个数组元素上最多应用一次给定的操作来使数组不减少。在一次操作中,可以将元素减少一个,即arr [i] = arr [i] – 1

例子:

方法:以递增的顺序处理元素,并在不使当前元素小于前一个元素的情况下尽可能地减少当前元素。 (因此,应该始终减少第一个元素。)如果当前元素在任何时候都小于前一个元素,那么无论执行什么操作,答案都是“否”。

此策略之所以是最佳策略,是因为减少数字将使其更容易(或至少很容易)处理列表中的下一个元素,因为当前一个数字较小时,下一个元素可能会采用的任何值仍然可以使用,实际上,减少前一个数字会扩大下一组的可能值范围。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to make array non-decreasing
bool isPossible(int a[], int n)
{
    // Take the first element
    int cur = a[0];
  
    // Perform the operation
    cur--;
  
    // Traverse the array
    for (int i = 1; i < n; i++) {
  
        // Next element
        int nxt = a[i];
  
        // If next element is greater than the
        // current element then decrease
        // it to increase the possibilities
        if (nxt > cur)
            nxt--;
  
        // It is not possible to make the
        // array non-decreasing with
        // the given operation
        else if (nxt < cur)
            return false;
  
        // Next element is now the current
        cur = nxt;
    }
  
    // The array can be made non-decreasing
    // with the given operation
    return true;
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 1, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
  
    if (isPossible(a, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to make array non-decreasing
static boolean isPossible(int a[], int n)
{
    // Take the first element
    int cur = a[0];
  
    // Perform the operation
    cur--;
  
    // Traverse the array
    for (int i = 1; i < n; i++)
    {
  
        // Next element
        int nxt = a[i];
  
        // If next element is greater than the
        // current element then decrease
        // it to increase the possibilities
        if (nxt > cur)
            nxt--;
  
        // It is not possible to make the
        // array non-decreasing with
        // the given operation
        else if (nxt < cur)
            return false;
  
        // Next element is now the current
        cur = nxt;
    }
  
    // The array can be made non-decreasing
    // with the given operation
    return true;
}
  
// Driver code
public static void main(String []args) 
{
    int a[] = { 1, 2, 1, 2, 3 };
    int n = a.length;
  
    if (isPossible(a, n))
        System.out.printf("Yes");
    else
        System.out.printf("No");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to make array non-decreasing 
def isPossible(a, n) :
  
    # Take the first element 
    cur = a[0]; 
  
    # Perform the operation 
    cur -= 1; 
  
    # Traverse the array 
    for i in range(1, n) : 
  
        # Next element 
        nxt = a[i]; 
  
        # If next element is greater than the 
        # current element then decrease 
        # it to increase the possibilities 
        if (nxt > cur) :
            nxt -= 1; 
  
        # It is not possible to make the 
        # array non-decreasing with 
        # the given operation 
        elif (nxt < cur) :
            return False; 
  
        # Next element is now the current 
        cur = nxt; 
  
    # The array can be made non-decreasing 
    # with the given operation 
    return True; 
  
# Driver code 
if __name__ == "__main__" : 
  
    a = [ 1, 2, 1, 2, 3 ]; 
    n = len(a); 
  
    if (isPossible(a, n)) :
        print("Yes"); 
    else :
        print("No"); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG
{
  
// Function to make array non-decreasing
static Boolean isPossible(int []a, int n)
{
    // Take the first element
    int cur = a[0];
  
    // Perform the operation
    cur--;
  
    // Traverse the array
    for (int i = 1; i < n; i++)
    {
  
        // Next element
        int nxt = a[i];
  
        // If next element is greater than the
        // current element then decrease
        // it to increase the possibilities
        if (nxt > cur)
            nxt--;
  
        // It is not possible to make the
        // array non-decreasing with
        // the given operation
        else if (nxt < cur)
            return false;
  
        // Next element is now the current
        cur = nxt;
    }
  
    // The array can be made non-decreasing
    // with the given operation
    return true;
}
  
// Driver code
public static void Main(String []args) 
{
    int []a = { 1, 2, 1, 2, 3 };
    int n = a.Length;
  
    if (isPossible(a, n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by 29AjayKumar


输出:
Yes