📌  相关文章
📜  排列数字以形成有效序列

📅  最后修改于: 2021-10-26 06:43:38             🧑  作者: Mango

给定一个具有 N 个不同数字的数组arr[]和另一个具有 N-1 个运算符(< 或 >)的数组arr1[] ,任务是组织这些数字以形成一个有效序列,该序列遵守关于提供的运算符的关系运算符规则.

例子:

天真的方法:
一种天真的方法是尝试所有可能的数字排列并检查序列是否有效。
时间复杂度: O(2 N )。

有效的方法:这个想法是首先按升序对给定的数字数组进行排序。然后使用两个指针技术解决问题:一个指向前面,另一个指向末尾。

  1. 取一个与给定数组大小相同的结果数组。
  2. 如果当前运算符是 ‘<‘,则将 top 指针指向的元素包含在结果数组中,并将其加 1。
  3. 如果当前运算符是“>”,则将最后一个指针指向的元素包含在结果数组中,并将其减 1。

下面是上述方法的实现。

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to organize the given numbers
// to form a valid sequence.
vector orgazineInOrder(vector vec,
                            vector op, int n)
{
    vector result(n);
    // Sorting the array
    sort(vec.begin(), vec.end());
 
    int i = 0, j = n - 1, k = 0;
    while (i <= j && k <= n - 2) {
        // Two pointer technique
        // to organize the numbers
        if (op[k] == '<') {
            result[k] = vec[i++];
        }
        else {
            result[k] = vec[j--];
        }
        k++;
    }
    result[n - 1] = vec[i];
 
    return result;
}
 
// Driver code
int main()
{
    vector vec({ 8, 2, 7, 1, 5, 9 });
 
    vector op({ '>', '>', '<',
                     '>', '<' });
 
    vector result
        = orgazineInOrder(vec,
                          op, vec.size());
 
    for (int i = 0; i < result.size(); i++) {
        cout << result[i] << " ";
    }
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
// Function to organize the given numbers
// to form a valid sequence.
static int[] orgazineInOrder(int []vec,int[] op, int n)
{
    int []result = new int[n];
     
    // Sorting the array
    Arrays.sort(vec);
 
    int i = 0, j = n - 1, k = 0;
    while (i <= j && k <= n - 2)
    {
        // Two pointer technique
        // to organize the numbers
        if (op[k] == '<')
        {
            result[k] = vec[i++];
        }
        else
        {
            result[k] = vec[j--];
        }
        k++;
    }
    result[n - 1] = vec[i];
 
    return result;
}
 
// Driver code
public static void main(String[] args)
{
    int []vec ={ 8, 2, 7, 1, 5, 9 };
 
    int[] op ={ '>', '>', '<',
                    '>', '<' };
 
    int []result = orgazineInOrder(vec,
                        op, vec.length);
 
    for (int i = 0; i < result.length; i++)
    {
        System.out.print(result[i]+ " ");
    }
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the above approach
 
# Function to organize the given numbers
# to form a valid sequence.
def orgazineInOrder(vec, op, n) :
 
    result = [0] * n;
     
    # Sorting the array
    vec.sort();
    i = 0;
    j = n - 1;
    k = 0;
     
    while (i <= j and k <= n - 2) :
         
        # Two pointer technique
        # to organize the numbers
        if (op[k] == '<') :
            result[k] = vec[i];
            i += 1;
         
        else :
            result[k] = vec[j];
            j -= 1;
         
        k += 1;
 
    result[n - 1] = vec[i];
 
    return result;
 
# Driver code
if __name__ == "__main__" :
 
    vec = [ 8, 2, 7, 1, 5, 9 ];
    op = [ '>', '>', '<', '>', '<' ];
 
    result = orgazineInOrder(vec, op, len(vec));
 
    for i in range(len(result)) :
        print(result[i], end = " ");
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to organize the given numbers
    // to form a valid sequence.
    static int[] orgazineInOrder(int []vec,int[] op, int n)
    {
        int []result = new int[n];
         
        // Sorting the array
        Array.Sort(vec);
     
        int i = 0, j = n - 1, k = 0;
        while (i <= j && k <= n - 2)
        {
            // Two pointer technique
            // to organize the numbers
            if (op[k] == '<')
            {
                result[k] = vec[i++];
            }
            else
            {
                result[k] = vec[j--];
            }
            k++;
        }
        result[n - 1] = vec[i];
     
        return result;
    }
     
    // Driver code
    public static void Main()
    {
        int []vec ={ 8, 2, 7, 1, 5, 9 };
     
        int[] op ={ '>', '>', '<',
                        '>', '<' };
     
        int []result = orgazineInOrder(vec,
                            op, vec.Length);
     
        for (int i = 0; i < result.Length; i++)
        {
            Console.Write(result[i] + " ");
        }
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
9 8 1 7 2 5

时间复杂度: O(NlogN)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程