📌  相关文章
📜  按字典顺序排列的前N个自然数的最小排列,具有K个完美指数

📅  最后修改于: 2021-04-17 18:25:32             🧑  作者: Mango

给定两个正整数NK ,任务是从字典上找到前N个自然数的最小排列,从而恰好有K个完美指数。

例子:

天真的方法:这个想法是生成所有第一个N个自然数的所有可能排列,并打印在字典上最小的,具有K个完美索引的该排列。
时间复杂度: O(N * N!)
辅助空间: O(1)

高效方法:为了优化上述方法,我们的想法是观察到最小的排列将以递增的顺序在前面排列范围为[1,N]的最后K个元素。其余元素可以按递增顺序附加。请按照以下步骤解决问题:

  • 初始化数组A []以存储按字典顺序排列的前N个自然数的最小排列。
  • 使用变量i遍历[0,K – 1]范围,并更新数组元素A [i]以存储(N – K + 1)+ i
  • 使用变量i[K,N – 1]范围内进行迭代,并将数组元素A [i]更新为(i – K + 1)
  • 完成上述步骤后,按字典顺序打印数组A [] ,该数组按字典顺序包含具有K个完善索引的最小排列。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the lexicographically
// smallest permutation with K perfect indices
void findPerfectIndex(int N, int K)
{
    // Iterator to traverse the array
    int i = 0;
 
    // Traverse first K array indices
    for (; i < K; i++) {
        cout << (N - K + 1) + i << " ";
    }
 
    // Traverse remaining indices
    for (; i < N; i++) {
        cout << i - K + 1 << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 10, K = 3;
    findPerfectIndex(N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to print the lexicographically
// smallest permutation with K perfect indices
static void findPerfectIndex(int N, int K)
{
   
    // Iterator to traverse the array
    int i = 0;
 
    // Traverse first K array indices
    for (; i < K; i++)
    {
        System.out.print((N - K + 1) + i+ " ");
    }
 
    // Traverse remaining indices
    for (; i < N; i++)
    {
        System.out.print(i - K + 1+ " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 10, K = 3;
    findPerfectIndex(N, K);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program for the above approach
 
# Function to print the lexicographically
# smallest permutation with K perfect indices
def findPerfectIndex(N, K) :
     
    # Iterator to traverse the array
    i = 0
 
    # Traverse first K array indices
    for i in range(K):
        print((N - K + 1) + i, end = " ")
     
 
    # Traverse remaining indices
    for i in range(3, N):
        print( i - K + 1, end = " ")
     
# Driver Code
 
N = 10
K = 3
findPerfectIndex(N, K)
 
# This code is contributed by code_hunt.


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to print the lexicographically
// smallest permutation with K perfect indices
static void findPerfectIndex(int N, int K)
{
   
    // Iterator to traverse the array
    int i = 0;
 
    // Traverse first K array indices
    for (; i < K; i++)
    {
        Console.Write((N - K + 1) + i+ " ");
    }
 
    // Traverse remaining indices
    for (; i < N; i++)
    {
        Console.Write(i - K + 1+ " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 10, K = 3;
    findPerfectIndex(N, K);
}
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
8 9 10 1 2 3 4 5 6 7

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