📌  相关文章
📜  将数组拆分为最少数量的非递增或非递减子数组

📅  最后修改于: 2021-05-17 17:27:58             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是将给定数组拆分为最小数量的子数组,以使每个子数组的元素以非递增顺序或非递减顺序排列。

例子:

方法:为了最小化子阵列的数量,每个子阵列的大小应最大化。可以通过贪婪地将元素放置在子数组中来完成。

请按照以下步骤解决问题:

  • 初始化变量,说ANS,具有1至存储所需的结果,并用N-电流,以保持当前的序列的顺序的轨迹,无论是非减(I),非增(d),或无(N )
  • 现在,在[1,N – 1]范围内迭代数组:
    • 如果电流等于N ,请执行以下操作:  
      • 如果arr [i] 则将电流更新为D。
      • 否则,如果arr [i]> arr [i-1] ,则将current更新为I。
      • 否则,将电流更新为N。
    • 如果 当前等于I ,执行以下操作:
      • 如果arr [i]≥arr[i-1],则将电流更新为I。
      • 否则,将电流更新为N并将ans增加1
    • 否则,请执行以下操作:
      • 如果arr [i]≤arr [i-1] ,则将电流更新为D。
      • 否则,将电流更新为N并将ans增加1
  • 完成上述步骤后,将ans的值打印为结果。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to split the array into minimum count
// of subarrays such that each subarray is either
// non-increasing or non-decreasing
void minimumSubarrays(int arr[], int n)
{
 
  // Initialize variable to keep
  // track of current sequence
  char current = 'N';
 
  // Stores the required result
  int answer = 1;
 
  // Traverse the array, arr[]
  for (int i = 1; i < n; i++) {
 
    // If current sequence is neither
    // non-increasing nor non-decreasing
    if (current == 'N') {
 
      // If previous element is greater
      if (arr[i] < arr[i - 1]) {
 
        // Update current
        current = 'D';
      }
 
      // If previous element is equal
      // to the current element
      else if (arr[i] == arr[i - 1]) {
 
        // Update current
        current = 'N';
      }
 
      // Otherwise
      else {
 
        // Update current
        current = 'I';
      }
    }
 
    // If current sequence
    // is in non-decreasing
    else if (current == 'I') {
 
      // If previous element is
      // less than or equal to
      // the current element
      if (arr[i] >= arr[i - 1]) {
        current = 'I';
      }
 
      // Otherwise
      else {
 
        // Update current as N and
        // increment answer by 1
        current = 'N';
        answer += 1;
      }
    }
 
    // If current sequence
    // is Non-Increasing
    else {
 
      // If previous element is
      // greater or equal to
      // the current element
      if (arr[i] <= arr[i - 1]) {
        current = 'D';
      }
 
      // Otherwise
      else {
 
        // Update current as N and
        // increment answer by 1
        current = 'N';
        answer += 1;
      }
    }
  }
 
  // Print the answer
  cout<


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to split the array into minimum count
    // of subarrays such that each subarray is either
    // non-increasing or non-decreasing
    static void minimumSubarrays(int[] arr, int n)
    {
 
        // Initialize variable to keep
        // track of current sequence
        char current = 'N';
 
        // Stores the required result
        int answer = 1;
 
        // Traverse the array, arr[]
        for (int i = 1; i < n; i++) {
 
            // If current sequence is neither
            // non-increasing nor non-decreasing
            if (current == 'N') {
 
                // If previous element is greater
                if (arr[i] < arr[i - 1]) {
 
                    // Update current
                    current = 'D';
                }
 
                // If previous element is equal
                // to the current element
                else if (arr[i] == arr[i - 1]) {
 
                    // Update current
                    current = 'N';
                }
 
                // Otherwise
                else {
 
                    // Update current
                    current = 'I';
                }
            }
 
            // If current sequence
            // is in non-decreasing
            else if (current == 'I') {
 
                // If previous element is
                // less than or equal to
                // the current element
                if (arr[i] >= arr[i - 1]) {
                    current = 'I';
                }
 
                // Otherwise
                else {
 
                    // Update current as N and
                    // increment answer by 1
                    current = 'N';
                    answer += 1;
                }
            }
 
            // If current sequence
            // is Non-Increasing
            else {
 
                // If previous element is
                // greater or equal to
                // the current element
                if (arr[i] <= arr[i - 1]) {
                    current = 'D';
                }
 
                // Otherwise
                else {
 
                    // Update current as N and
                    // increment answer by 1
                    current = 'N';
                    answer += 1;
                }
            }
        }
 
        // Print the answer
        System.out.print(answer);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given array
        int arr[] = { 2, 3, 9, 5, 4, 6, 8 };
 
        // Given size of array
        int n = arr.length;
 
        minimumSubarrays(arr, n);
    }
}


Python3
# Python3 program for the above approach
 
# Function to split the array into minimum count
# of subarrays such that each subarray is either
# non-increasing or non-decreasing
def minimumSubarrays(arr, n):
 
    # Initialize variable to keep
    # track of current sequence
    current = 'N'
 
    # Stores the required result
    answer = 1
 
    # Traverse the array, arr[]
    for i in range(1, n):
 
        # If current sequence is neither
        # non-increasing nor non-decreasing
        if (current == 'N'):
 
            # If previous element is greater
            if (arr[i] < arr[i - 1]):
 
                # Update current
                current = 'D'
 
            # If previous element is equal
            # to the current element
            elif (arr[i] == arr[i - 1]):
 
                # Update current
                current = 'N'
 
            # Otherwise
            else:
 
                # Update current
                current = 'I'
 
        # If current sequence
        # is in non-decreasing
        elif (current == 'I'):
 
            #I f previous element is
            # less than or equal to
            # the current element
            if (arr[i] >= arr[i - 1]):
                current = 'I'
 
            # Otherwise
            else:
 
                # Update current as N and
                # increment answer by 1
                current = 'N'
                answer += 1
 
        # If current sequence
        # is Non-Increasing
        else:
 
            # If previous element is
            # greater or equal to
            # the current element
            if (arr[i] <= arr[i - 1]):
                current = 'D'
 
            # Otherwise
            else:
 
                # Update current as N and
                # increment answer by 1
                current = 'N'
                answer += 1
 
    # Print the answer
    print(answer)
 
# Driver Code
if __name__ == '__main__':
 
    # Given array
    arr = [ 2, 3, 9, 5, 4, 6, 8 ]
 
    # Given size of array
    n = len(arr)
     
    minimumSubarrays(arr, n)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to split the array into minimum count
    // of subarrays such that each subarray is either
    // non-increasing or non-decreasing
    static void minimumSubarrays(int[] arr, int n)
    {
 
        // Initialize variable to keep
        // track of current sequence
        char current = 'N';
 
        // Stores the required result
        int answer = 1;
 
        // Traverse the array, []arr
        for (int i = 1; i < n; i++) {
 
            // If current sequence is neither
            // non-increasing nor non-decreasing
            if (current == 'N') {
 
                // If previous element is greater
                if (arr[i] < arr[i - 1]) {
 
                    // Update current
                    current = 'D';
                }
 
                // If previous element is equal
                // to the current element
                else if (arr[i] == arr[i - 1]) {
 
                    // Update current
                    current = 'N';
                }
 
                // Otherwise
                else {
 
                    // Update current
                    current = 'I';
                }
            }
 
            // If current sequence
            // is in non-decreasing
            else if (current == 'I') {
 
                // If previous element is
                // less than or equal to
                // the current element
                if (arr[i] >= arr[i - 1]) {
                    current = 'I';
                }
 
                // Otherwise
                else {
 
                    // Update current as N and
                    // increment answer by 1
                    current = 'N';
                    answer += 1;
                }
            }
 
            // If current sequence
            // is Non-Increasing
            else {
 
                // If previous element is
                // greater or equal to
                // the current element
                if (arr[i] <= arr[i - 1]) {
                    current = 'D';
                }
 
                // Otherwise
                else {
 
                    // Update current as N and
                    // increment answer by 1
                    current = 'N';
                    answer += 1;
                }
            }
        }
 
        // Print the answer
        Console.Write(answer);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
 
        // Given array
        int []arr = { 2, 3, 9, 5, 4, 6, 8 };
 
        // Given size of array
        int n = arr.Length;
 
        minimumSubarrays(arr, n);
    }
}
 
// This code is contributed by 29AjayKumar


输出:
3

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