具有给定数组作为子序列的字典上最小的排列数,直到 K
给定一个整数K和一个数组arr[]在[1, K]范围内有N个成对不同的整数,任务是找到前K个正整数的字典序最小排列,使得给定数组arr[]是一个子序列的排列。
例子:
Input: arr[] = {1, 3, 5, 7}, K = 8
Output: 1 2 3 4 5 6 7 8
Explanation: {1, 2, 3, 4, 5, 6, 7, 8} is the lexicographically smallest permutation of the first 8 positive integers such that the given array {1, 3, 5, 7} is also a subsequence of the permutation.
Input: arr[] = {6, 4, 2, 1}, K=7
Output: 3 5 6 4 2 1 7
方法:给定的问题可以通过使用贪心方法来解决。以下是要遵循的步骤:
- 使用本文讨论的方法创建一个向量missing[] ,以升序存储范围[1, K]中不存在于给定数组arr[]中的整数。
- 创建两个指针p1和p2 ,它们分别将当前索引存储在arr[]和missing[]中。最初,两者都等于0 。
- 贪婪地将arr[p1]和缺少的 [p2]的最小值存储到一个向量中,比如ans[]并将相应的指针递增到下一个位置,直到存储的整数的计数小于K 。
- 为了使事情变得更容易,在数组missing[]和arr[]的末尾附加 INT_MAX 可以避免越界。
- 完成上述步骤后, ans[]中存储的所有值都是需要的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the lexicographically
// smallest permutation such that the
// given array is a subsequence of it
void findPermutation(int K, vector arr)
{
// Stores the missing elements in
// arr in the range [1, K]
vector missing;
// Stores if the ith element is
// present in arr or not
vector visited(K + 1, 0);
// Loop to mark all integers present
// in the array as visited
for (int i = 0; i < arr.size(); i++) {
visited[arr[i]] = 1;
}
// Loop to insert all the integers
// not visited into missing
for (int i = 1; i <= K; i++) {
if (!visited[i]) {
missing.push_back(i);
}
}
// Append INT_MAX at end in order
// to prevent going out of bounds
arr.push_back(INT_MAX);
missing.push_back(INT_MAX);
// Pointer to the current element
int p1 = 0;
// Pointer to the missing element
int p2 = 0;
// Stores the required permutation
vector ans;
// Loop to construct the permutation
// using greedy approach
while (ans.size() < K) {
// If missing element is smaller
// that the current element insert
// missing element
if (arr[p1] < missing[p2]) {
ans.push_back(arr[p1]);
p1++;
}
// Insert current element
else {
ans.push_back(missing[p2]);
p2++;
}
}
// Print the required Permutation
for (int i = 0; i < K; i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int main()
{
int K = 7;
vector arr = { 6, 4, 2, 1 };
// Function Call
findPermutation(K, arr);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the lexicographically
// smallest permutation such that the
// given array is a subsequence of it
static void findPermutation(int K, Vector arr)
{
// Stores the missing elements in
// arr in the range [1, K]
Vector missing = new Vector();
// Stores if the ith element is
// present in arr or not
boolean visited[] = new boolean[K + 1];
// Loop to mark all integers present
// in the array as visited
for (int i = 0; i < arr.size(); i++) {
visited[arr.get(i)] = true;
}
// Loop to insert all the integers
// not visited into missing
for (int i = 1; i <= K; i++) {
if (!visited[i]) {
missing.add(i);
}
}
// Append Integer.MAX_VALUE at end in order
// to prevent going out of bounds
arr.add(Integer.MAX_VALUE);
missing.add(Integer.MAX_VALUE);
// Pointer to the current element
int p1 = 0;
// Pointer to the missing element
int p2 = 0;
// Stores the required permutation
Vector ans = new Vector();
// Loop to construct the permutation
// using greedy approach
while (ans.size() < K) {
// If missing element is smaller
// that the current element insert
// missing element
if (arr.get(p1) < missing.get(p2)) {
ans.add(arr.get(p1));
p1++;
}
// Insert current element
else {
ans.add(missing.get(p2));
p2++;
}
}
// Print the required Permutation
for (int i = 0; i < K; i++) {
System.out.print(ans.get(i)+ " ");
}
}
// Driver Code
public static void main(String[] args)
{
int K = 7;
Integer []a = {6, 4, 2, 1};
Vector arr = new Vector<>(Arrays.asList(a));
// Function Call
findPermutation(K, arr);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find the lexicographically
# smallest permutation such that the
# given array is a subsequence of it
def findPermutation(K, arr):
# Stores the missing elements in
# arr in the range [1, K]
missing = []
# Stores if the ith element is
# present in arr or not
visited = [0]*(K+1)
# Loop to mark all integers present
# in the array as visited
for i in range(4):
visited[arr[i]] = 1
# Loop to insert all the integers
# not visited into missing
for i in range(1, K+1):
if(not visited[i]):
missing.append(i)
# Append INT_MAX at end in order
# to prevent going out of bounds
INT_MAX = 2147483647
arr.append(INT_MAX)
missing.append(INT_MAX)
# Pointer to the current element
p1 = 0
# Pointer to the missing element
p2 = 0
# Stores the required permutation
ans = []
# Loop to construct the permutation
# using greedy approach
while (len(ans) < K):
# If missing element is smaller
# that the current element insert
# missing element
if (arr[p1] < missing[p2]):
ans.append(arr[p1])
p1 = p1 + 1
# Insert current element
else:
ans.append(missing[p2])
p2 = p2 + 1
# Print the required Permutation
for i in range(0, K):
print(ans[i], end=" ")
# Driver Code
if __name__ == "__main__":
K = 7
arr = [6, 4, 2, 1]
# Function Call
findPermutation(K, arr)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG{
// Function to find the lexicographically
// smallest permutation such that the
// given array is a subsequence of it
static void findPermutation(int K, ArrayList arr)
{
// Stores the missing elements in
// arr in the range [1, K]
ArrayList missing = new ArrayList();
// Stores if the ith element is
// present in arr or not
bool [] visited = new bool[K + 1];
// Loop to mark all integers present
// in the array as visited
for (int i = 0; i < arr.Count; i++) {
visited[(int)arr[i]] = true;
}
// Loop to insert all the integers
// not visited into missing
for (int i = 1; i <= K; i++) {
if (!visited[i]) {
missing.Add(i);
}
}
// Append Int32.MaxValue at end in order
// to prevent going out of bounds
arr.Add(Int32.MaxValue);
missing.Add(Int32.MaxValue);
// Pointer to the current element
int p1 = 0;
// Pointer to the missing element
int p2 = 0;
// Stores the required permutation
ArrayList ans = new ArrayList();
// Loop to construct the permutation
// using greedy approach
while (ans.Count < K) {
// If missing element is smaller
// that the current element insert
// missing element
if ((int)arr[p1] < (int)missing[p2]) {
ans.Add(arr[p1]);
p1++;
}
// Insert current element
else {
ans.Add(missing[p2]);
p2++;
}
}
// Print the required Permutation
for (int i = 0; i < K; i++) {
Console.Write(ans[i]+ " ");
}
}
// Driver Code
public static void Main()
{
int K = 7;
int [] a = {6, 4, 2, 1};
ArrayList arr = new ArrayList(a);
// Function Call
findPermutation(K, arr);
}
}
// This code is contributed by ihritik.
Javascript
输出:
3 5 6 4 2 1 7
时间复杂度: O(N)
辅助空间: O(N)