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

📅  最后修改于: 2021-04-26 09:08:37             🧑  作者: Mango

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

例子:

天真的方法:
天真的方法是尝试所有可能的数字排列方式,并检查序列是否有效。

时间复杂度: O(2 N )。

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

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

下面是上述方法的实现。

C++
// C++ implemenattion 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 implemenattion 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 implemenattion 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


输出:
9 8 1 7 2 5

时间复杂度: O(NlogN)