📜  使数组排序所需的最小双端队列数

📅  最后修改于: 2022-05-13 01:56:04.826000             🧑  作者: Mango

使数组排序所需的最小双端队列数

给定一个包含N个唯一整数的数组arr 。任务是计算 所需的最少双端队列数 使数组排序。

示例

方法:上述问题可以贪婪地解决。请按照以下步骤解决问题:

  1. 创建两个数组frontsbacks将存储所有双端队列的前后元素。
  2. 迭代数组中的所有元素。对于每个元素arr[i] ,如果满足以下条件,则可以将当前元素推送到预先存在的双端队列中:
    • 存在一个大于arr[i] 的 fronts[j]因为这意味着这个arr[i]可以被推到这个双端队列的前面。但是如果在数组arr中的arr[i]fronts[j]之间存在另一个元素,那么它就不能被推送,因为推送会扰乱 deque 中元素的顺序,使得这些 deque 不能以排序的形式排列数组,甚至单独排序。
    • 同样,使用上述步骤检查阵列背面
  3. 如果一个元素不能被推送到双端队列中,那么应该为该元素创建另一个双端队列。因此,将元素推送到 fronts 和 backs 数组中,因为它既是新创建的双端队列的前面又是后面。
  4. 现在,返回数组正面(或背面)的大小作为此问题的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
int minDeques(vector arr)
{
    // Vectors to store the front and back
    // element of all present deques
    vector fronts, backs;
 
    // Iterate through all array elements
    for (int i = 0; i < arr.size(); i++) {
 
        // Bool variable to check if the array
        // element has already been pushed or not
        bool hasBeenPushed = false;
 
        for (int j = 0; j < fronts.size(); j++) {
 
            // Check for all deques where arr[i]
            // can be pushed in the front
            if (arr[i] < fronts[j]) {
                bool isSafe = true;
                for (int k = 0; k < arr.size(); k++) {
                    if (arr[i] < arr[k]
                        && arr[k] < fronts[j]) {
                        isSafe = false;
                        break;
                    }
                }
 
                // Push in front of an already
                // existing deque
                if (isSafe) {
                    fronts[j] = arr[i];
                    hasBeenPushed = true;
                    break;
                }
            }
 
            // Check for all deques where arr[i]
            // can be pushed in the back
            if (arr[i] > backs[j]) {
                bool isSafe = true;
                for (int k = 0; k < arr.size(); k++) {
                    if (arr[i] > arr[k]
                        && arr[k] > backs[j]) {
                        isSafe = false;
                        break;
                    }
                }
 
                // Push in back of an already
                // existing deque
                if (isSafe) {
                    backs[j] = arr[i];
                    hasBeenPushed = true;
                    break;
                }
            }
        }
 
        // If arr[i] cannot be pushed to any
        // of the existing deques, then push
        // it in a new deque
        if (!hasBeenPushed) {
            fronts.push_back(arr[i]);
            backs.push_back(arr[i]);
        }
    }
 
    return fronts.size();
}
 
// Driver Code
int main()
{
    vector arr = { 3, 6, 0, 9, 5, 4 };
    cout << minDeques(arr);
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
public static int minDeques(int[] arr)
{
    // Vectors to store the front and back
    // element of all present deques
    ArrayList fronts = new ArrayList();
    ArrayList backs = new ArrayList();
 
    // Iterate through all array elements
    for (int i = 0; i < arr.length; i++) {
 
        // Bool variable to check if the array
        // element has already been pushed or not
        boolean hasBeenPushed = false;
 
        for (int j = 0; j < fronts.size(); j++) {
 
            // Check for all deques where arr[i]
            // can be pushed in the front
            if (arr[i] < fronts.get(j)) {
                boolean isSafe = true;
                for (int k = 0; k < arr.length; k++) {
                    if (arr[i] < arr[k]
                        && arr[k] < fronts.get(j)) {
                        isSafe = false;
                        break;
                    }
                }
 
                // Push in front of an already
                // existing deque
                if (isSafe) {
                    fronts.set(j, arr[i]);
                    hasBeenPushed = true;
                    break;
                }
            }
 
            // Check for all deques where arr[i]
            // can be pushed in the back
            if (arr[i] > backs.get(j)) {
                Boolean isSafe = true;
                for (int k = 0; k < arr.length; k++) {
                    if (arr[i] > arr[k]
                        && arr[k] > backs.get(j)) {
                        isSafe = false;
                        break;
                    }
                }
 
                // Push in back of an already
                // existing deque
                if (isSafe) {
                    backs.set(j, arr[i]);
                    hasBeenPushed = true;
                    break;
                }
            }
        }
 
        // If arr[i] cannot be pushed to any
        // of the existing deques, then push
        // it in a new deque
        if (!hasBeenPushed) {
            fronts.add(arr[i]);
            backs.add(arr[i]);
        }
    }
 
    return fronts.size();
}
 
// Driver Code
public static void main(String args[])
{
    int[] arr = { 3, 6, 0, 9, 5, 4 };
    System.out.println(minDeques(arr));
}
}
 
// This code is contributed by saurabh_jaiswal..


Python3
# python program for the above approach
def minDeques(arr):
 
        # Vectors to store the front and back
        # element of all present deques
    fronts = []
    backs = []
 
    # Iterate through all array elements
    for i in range(0, len(arr)):
 
                # Bool variable to check if the array
                # element has already been pushed or not
        hasBeenPushed = False
 
        for j in range(0, len(fronts)):
 
                        # Check for all deques where arr[i]
                        # can be pushed in the front
            if (arr[i] < fronts[j]):
                isSafe = True
                for k in range(0, len(arr)):
                    if (arr[i] < arr[k] and arr[k] < fronts[j]):
                        isSafe = False
                        break
 
                        # Push in front of an already
                        # existing deque
                if (isSafe):
                    fronts[j] = arr[i]
                    hasBeenPushed = True
                    break
 
                    # Check for all deques where arr[i]
                    # can be pushed in the back
            if (arr[i] > backs[j]):
                isSafe = True
                for k in range(0, len(arr)):
                    if (arr[i] > arr[k] and arr[k] > backs[j]):
                        isSafe = False
                        break
 
                        # Push in back of an already
                        # existing deque
                if (isSafe):
                    backs[j] = arr[i]
                    hasBeenPushed = True
                    break
 
                # If arr[i] cannot be pushed to any
                # of the existing deques, then push
                # it in a new deque
        if (not hasBeenPushed):
            fronts.append(arr[i])
            backs.append(arr[i])
 
    return len(fronts)
 
 
# Driver Code
if __name__ == "__main__":
 
    arr = [3, 6, 0, 9, 5, 4]
    print(minDeques(arr))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
 
    static int minDeques(List arr)
    {
       
        // Vectors to store the front and back
        // element of all present deques
        List fronts = new List();
        List backs = new List();
 
        // Iterate through all array elements
        for (int i = 0; i < arr.Count; i++) {
 
            // Bool variable to check if the array
            // element has already been pushed or not
            bool hasBeenPushed = false;
 
            for (int j = 0; j < fronts.Count; j++) {
 
                // Check for all deques where arr[i]
                // can be pushed in the front
                if (arr[i] < fronts[j]) {
                    bool isSafe = true;
                    for (int k = 0; k < arr.Count; k++) {
                        if (arr[i] < arr[k]
                            && arr[k] < fronts[j]) {
                            isSafe = false;
                            break;
                        }
                    }
 
                    // Push in front of an already
                    // existing deque
                    if (isSafe) {
                        fronts[j] = arr[i];
                        hasBeenPushed = true;
                        break;
                    }
                }
 
                // Check for all deques where arr[i]
                // can be pushed in the back
                if (arr[i] > backs[j]) {
                    bool isSafe = true;
                    for (int k = 0; k < arr.Count; k++) {
                        if (arr[i] > arr[k]
                            && arr[k] > backs[j]) {
                            isSafe = false;
                            break;
                        }
                    }
 
                    // Push in back of an already
                    // existing deque
                    if (isSafe) {
                        backs[j] = arr[i];
                        hasBeenPushed = true;
                        break;
                    }
                }
            }
 
            // If arr[i] cannot be pushed to any
            // of the existing deques, then push
            // it in a new deque
            if (!hasBeenPushed) {
                fronts.Add(arr[i]);
                backs.Add(arr[i]);
            }
        }
 
        return fronts.Count;
    }
 
    // Driver Code
    public static void Main()
    {
        List arr = new List{ 3, 6, 0, 9, 5, 4 };
        Console.WriteLine(minDeques(arr));
    }
}
 
// This code is contributed by ukasp.


Javascript



输出
2

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