📜  大小 A 的字典序最小排列,其中 B 个整数超过所有前面的整数

📅  最后修改于: 2021-09-06 05:20:59             🧑  作者: Mango

给定两个正整数AB ,任务是生成直到A的所有整数的字典序最小排列,其中恰好B 个整数大于其所有前面的元素。

例子:

处理方法:按照以下步骤解决问题:

  1. 使用itertools库中的内置函数permutations()[1, A]生成所有可能的整数排列。基本上,它返回一个包含所有可能排列的元组。
  2. 现在,检查数组的最大元素和元素数量是否应满足问题的条件 count 是否相等。
  3. 如果相等,则只有一个可能的元素列表满足条件。从逻辑上讲,它们只是按排序顺序从 1 开始多个整数的范围。
  4. 如果不是,则从排列列表中取出每个元组,然后计算元组中存在的整数的数量,该数量大于该元组中所有先前的整数。
  5. 如果计数等于B,则将该元组加载到另一个新列表。
  6. 测试由新列表加载的元素列表是否按字典顺序排列,如果未排序,则手动排列它们并返回其中的最小值。

下面是上述方法的实现:

Python3
# Python3 program for the above approach
from itertools import permutations
 
# Function to find lexicographically
# smallest permutation of [1, A]
# having B integers greater than
# all the previous elements
 
def getSmallestArray(A, B):
 
    # if A and B are equal
    if A == B:
        return list(range(1, A + 1))
 
    # Initialize pos_list to store list
    # of elements which are satisfying
    # the given conditions
    pos_list = []
 
    # List to store all possible permutations
    Main_List = list(permutations(range(1, A + 1), A))
 
    # Check all the permutations for
    # the given condition
    for L in Main_List:
 
        # Stores the count of elements
        # greater than all the previous
        # elements
        c = 0
 
        i = 0
        j = 1
        while j <= (len(L)-1):
 
            if L[j] > L[i]:
                i += 1
 
            elif i == j:
                c += 1
                j += 1
                i = 0
 
            else:
                j += 1
                i = 0
 
        # If count is equal to B
        if (c + 1) == B:
 
            pos_list.append(list(L))
 
    # If only one tuple satisfies
    # the given condition
    if len(pos_list) == 1:
 
        return pos_list[0]
 
    # Otherwise
    satisfied_list = []
    for i in pos_list:
 
        array_test = ''.join(map(str, i))
        satisfied_list.append(int(array_test))
 
        # Evaluate lexicographically smallest tuple
    small = min(satisfied_list)
    str_arr = list(str(small))
    ans = list(map(int, str_arr))
 
    # Return the answer
    return ans
 
 
# Driver Code
 
A = 3
B = 2
 
print(getSmallestArray(A, B))


C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to find lexicographically
// smallest permutation of [1, A]
// having B integers greater than
// all the previous elements
vector getSmallestArray(int A, int B)
{
     
    // Initial array
    vectorarr(A);
    for(int i = 0; i < A; i++)
        arr[i] = i + 1;
         
    // Resultant array
    vectorans(A);
 
    // Append the first (B-1) elements
    // to the resultant array 
    for(int i = 0; i < B - 1; i++)
        ans[i] = arr[i];
         
    // Append the maximum from the
    // initial array 
    ans[B - 1] = arr[A - 1];
     
    // Copy the remaining elements 
    for(int i = B; i < A; i++)
        ans[i] = arr[i - 1];
         
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    int A = 3;
    int B = 2;
     
    vectorans = getSmallestArray(A, B);
     
    for(auto i : ans)
        cout << i << " ";
}
 
// This code is contributed by Stream_Cipher


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find lexicographically
// smallest permutation of [1, A]
// having B integers greater than
// all the previous elements
@SuppressWarnings("unchecked")
static void getSmallestArray(int A, int B)
{
     
    // Initial array
    Vector arr = new Vector();
    for(int i = 0; i < A; i++)
        arr.add(i + 1);
         
    // Resultant array
    Vector ans = new Vector();
 
    // Append the first (B-1) elements
    // to the resultant array 
    for(int i = 0; i < B - 1; i++)
        ans.add(arr.get(i));
         
    // Append the maximum from the
    // initial array 
    ans.add(arr.get(A - 1));
     
    // Copy the remaining elements 
    for(int i = B; i < A; i++)
        ans.add(arr.get(i - 1));
         
    // Print the answer
    for(int i = 0; i < ans.size(); i++)
        System.out.print(ans.get(i) + " ");
}
 
// Driver Code
public static void main(String args[])
{
    int A = 3;
    int B = 2;
     
    getSmallestArray(A, B);
}
}
 
// This code is contributed by Stream_Cipher


Python3
# Python3 program for the above approach
 
# Function to find lexicographically
# smallest permutation of [1, A]
# having B integers greater than
# all the previous elements
 
def getSmallestArray(A, B):
 
    # Initial array
    arr = (list(range(1, (A + 1))))
 
    # Resultant array
    ans = []
 
    # Append the first (B-1) elements
    # to the resultant array
    ans[0:B-1] = arr[0:B-1]
 
    # Delete the appended elements
    # from the initial array
    del arr[0:B-1]
 
    # Append the maximum from the
    # initial array
    ans.append(arr[-1])
 
    # Delete the appended maximum
    del arr[-1]
 
    # Copy the remaining elements
    ans[B:] = arr[:]
 
    # Return the answer
    return ans
 
# Driver Code
 
A = 3
B = 2
 
print(getSmallestArray(A, B))


C#
// C# program for the above approach
using System.Collections.Generic;
using System;
 
class GFG{
 
// Function to find lexicographically
// smallest permutation of [1, A]
// having B integers greater than
// all the previous elements
 static void getSmallestArray(int A, int B)
{
     
    // Initial array
    List arr = new List();
    for(int i = 0; i < A; i++)
        arr.Add(i + 1);
         
    // Resultant array
    List ans = new List();
 
    // Append the first (B-1) elements
    // to the resultant array 
    for(int i = 0; i < B - 1; i++)
        ans.Add(arr[i]);
         
    // Append the maximum from the
    // initial array 
    ans.Add(arr[A - 1]);
     
    // Copy the remaining elements 
    for(int i = B; i < A; i++)
        ans.Add(arr[i - 1]);
         
    // Print the answer
    for(int i = 0; i < A; i++)
        Console.Write(ans[i] + " ");
}
 
// Driver Code
public static void Main()
{
    int A = 3;
    int B = 2;
     
    getSmallestArray(A,B);
}
}
 
// This code is contributed by Stream_Cipher


Javascript


输出:
[1, 3, 2]

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

高效方法:按照以下步骤优化上述方法:

  1. 初始化一个数组arr[]依次由第一个 A自然数组成。
  2. 创建一个结果数组ans[]并附加来自arr[]的前(B – 1) 个元素。
  3. 所以结果数组有(B – 1) 个满足给定条件的元素。
  4. 现在将arr[] 中的最大元素插入到结果数组中。移除插入的元素arr[]
  5. 由于我们已经有B 个元素,因此结果数组有B 个满足给定条件的元素。
  6. 现在,将所有剩余元素从arr[]一一复制到结果数组并打印结果数组。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include
using namespace std;
 
// Function to find lexicographically
// smallest permutation of [1, A]
// having B integers greater than
// all the previous elements
vector getSmallestArray(int A, int B)
{
     
    // Initial array
    vectorarr(A);
    for(int i = 0; i < A; i++)
        arr[i] = i + 1;
         
    // Resultant array
    vectorans(A);
 
    // Append the first (B-1) elements
    // to the resultant array 
    for(int i = 0; i < B - 1; i++)
        ans[i] = arr[i];
         
    // Append the maximum from the
    // initial array 
    ans[B - 1] = arr[A - 1];
     
    // Copy the remaining elements 
    for(int i = B; i < A; i++)
        ans[i] = arr[i - 1];
         
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    int A = 3;
    int B = 2;
     
    vectorans = getSmallestArray(A, B);
     
    for(auto i : ans)
        cout << i << " ";
}
 
// This code is contributed by Stream_Cipher

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find lexicographically
// smallest permutation of [1, A]
// having B integers greater than
// all the previous elements
@SuppressWarnings("unchecked")
static void getSmallestArray(int A, int B)
{
     
    // Initial array
    Vector arr = new Vector();
    for(int i = 0; i < A; i++)
        arr.add(i + 1);
         
    // Resultant array
    Vector ans = new Vector();
 
    // Append the first (B-1) elements
    // to the resultant array 
    for(int i = 0; i < B - 1; i++)
        ans.add(arr.get(i));
         
    // Append the maximum from the
    // initial array 
    ans.add(arr.get(A - 1));
     
    // Copy the remaining elements 
    for(int i = B; i < A; i++)
        ans.add(arr.get(i - 1));
         
    // Print the answer
    for(int i = 0; i < ans.size(); i++)
        System.out.print(ans.get(i) + " ");
}
 
// Driver Code
public static void main(String args[])
{
    int A = 3;
    int B = 2;
     
    getSmallestArray(A, B);
}
}
 
// This code is contributed by Stream_Cipher

蟒蛇3

# Python3 program for the above approach
 
# Function to find lexicographically
# smallest permutation of [1, A]
# having B integers greater than
# all the previous elements
 
def getSmallestArray(A, B):
 
    # Initial array
    arr = (list(range(1, (A + 1))))
 
    # Resultant array
    ans = []
 
    # Append the first (B-1) elements
    # to the resultant array
    ans[0:B-1] = arr[0:B-1]
 
    # Delete the appended elements
    # from the initial array
    del arr[0:B-1]
 
    # Append the maximum from the
    # initial array
    ans.append(arr[-1])
 
    # Delete the appended maximum
    del arr[-1]
 
    # Copy the remaining elements
    ans[B:] = arr[:]
 
    # Return the answer
    return ans
 
# Driver Code
 
A = 3
B = 2
 
print(getSmallestArray(A, B))

C#

// C# program for the above approach
using System.Collections.Generic;
using System;
 
class GFG{
 
// Function to find lexicographically
// smallest permutation of [1, A]
// having B integers greater than
// all the previous elements
 static void getSmallestArray(int A, int B)
{
     
    // Initial array
    List arr = new List();
    for(int i = 0; i < A; i++)
        arr.Add(i + 1);
         
    // Resultant array
    List ans = new List();
 
    // Append the first (B-1) elements
    // to the resultant array 
    for(int i = 0; i < B - 1; i++)
        ans.Add(arr[i]);
         
    // Append the maximum from the
    // initial array 
    ans.Add(arr[A - 1]);
     
    // Copy the remaining elements 
    for(int i = B; i < A; i++)
        ans.Add(arr[i - 1]);
         
    // Print the answer
    for(int i = 0; i < A; i++)
        Console.Write(ans[i] + " ");
}
 
// Driver Code
public static void Main()
{
    int A = 3;
    int B = 2;
     
    getSmallestArray(A,B);
}
}
 
// This code is contributed by Stream_Cipher

Javascript


输出:
[1, 3, 2]

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