📜  在大小上按字法排列的最小排列,具有大于所有先前整数的B个整数

📅  最后修改于: 2021-05-04 19:11:39             🧑  作者: Mango

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

例子:

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

  1. 使用itertools库中的内置函数permutations()[1,A]生成所有可能的整数置换。基本上,它返回一个由所有可能排列组成的元组。
  2. 现在,检查数组的最大元素和元素数量是否应满足问题的条件计数是否相等。
  3. 如果相等,则只有一个可能满足条件的元素列表。从逻辑上讲,它们只是按顺序从1开始的A整数范围。
  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


输出:
[1, 3, 2]









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

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

  1. 依次初始化由第一个A自然数组成的数组arr []
  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

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
输出:
[1, 3, 2]









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