通过在末端顺序插入 Array 元素,按字典顺序排列最大
给定一个包含N个整数的数组arr[] ,任务是通过将数组元素顺序插入另一个数组的前面或后面来找到字典顺序上最大的排列。
例子:
Input: arr[] = {3, 1, 2, 4}
Output: 4 3 1 2
Explanation:
The permutations that can be created by sequentially inserting the array elements to the front or the back of the container are {3, 1, 2, 4}, {1, 3, 2, 4}, {2, 3, 1, 4}, {2, 1, 3, 4}, {4, 1, 3, 2}, {4, 2, 3, 1}, {4, 2, 1, 3}, and {4, 3, 1, 2}. Out of which {4, 3, 1, 2} is the lexicographically largest permutation.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 5 4 3 2 1
方法:给定的问题可以通过使用双端队列的贪婪方法来解决,该方法基于以下观察:如果当前数组元素至少是新数组的第一个元素,则最佳选择总是将该元素插入容器的前面,以便按字典顺序最大化排列。否则,将元素插入到数组的末尾。请按照以下步骤解决给定的问题:
- 初始化一个双端队列,比如DQ ,它存储容器的当前状态。
- 初始化一个变量,比如mx ,它存储最大值,直到每个索引表示 deque DQ的第一个元素。
- 遍历给定数组arr[]如果当前元素arr[i] >= mx ,则将arr[i]插入到双端队列DQ的前面并更新mx的值。否则,将arr[i]插入到 deque DQ的后面。
- 完成上述步骤后,将存储在 deque DQ中的元素打印为结果最大排列。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the lexicographically
// largest permutation by sequentially
// inserting the array elements
void largestPermutation(int arr[], int N)
{
// Stores the current state of
// the new array
deque p;
// Stores the current maximum
// element of array arr[]
int mx = arr[0];
p.push_back(arr[0]);
// Iterate the array elements
for (int i = 1; i < N; i++) {
// If the current element is
// smaller than the current
// maximum, then insert
if (arr[i] < mx)
p.push_back(arr[i]);
// If the current element is
// at least the current maximum
else {
p.push_front(arr[i]);
// Update the value of
// the current maximum
mx = arr[i];
}
}
// Print resultant permutation
for (auto i : p)
cout << i << " ";
}
// Driver Code
int main()
{
int arr[] = { 3, 1, 2, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
largestPermutation(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG {
// Function to find the lexicographically
// largest permutation by sequentially
// inserting the array elements
static void largestPermutation(int arr[], int N)
{
// Stores the current state of
// the new array
Deque p = new LinkedList();
// Stores the current maximum
// element of array arr[]
int mx = arr[0];
p.addLast(arr[0]);
// Iterate the array elements
for (int i = 1; i < N; i++) {
// If the current element is
// smaller than the current
// maximum, then insert
if (arr[i] < mx)
p.addLast(arr[i]);
// If the current element is
// at least the current maximum
else {
p.addFirst(arr[i]);
// Update the value of
// the current maximum
mx = arr[i];
}
}
// Print resultant permutation
for (Iterator itr = p.iterator(); itr.hasNext();) {
System.out.print(itr.next() + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 1, 2, 4 };
int N = arr.length;
largestPermutation(arr, N);
}
}
// This code is contributed by Potta Lokesh
Python3
# python program for the above approach
# Function to find the lexicographically
# largest permutation by sequentially
# inserting the array elements
from typing import Deque
def largestPermutation(arr, N):
# Stores the current state of
# the new array
p = Deque()
# Stores the current maximum
# element of array arr[]
mx = arr[0]
p.append(arr[0])
# Iterate the array elements
for i in range(1, N):
# If the current element is
# smaller than the current
# maximum, then insert
if (arr[i] < mx):
p.append(arr[i])
# If the current element is
# at least the current maximum
else:
p.appendleft(arr[i])
# Update the value of
# the current maximum
mx = arr[i]
# Print resultant permutation
for i in p:
print(i, end=" ")
# Driver Code
if __name__ == "__main__":
arr = [3, 1, 2, 4]
N = len(arr)
largestPermutation(arr, N)
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG {
// Function to find the lexicographically
// largest permutation by sequentially
// inserting the array elements
static void largestPermutation(int []arr, int N) {
// Stores the current state of
// the new array
List p = new List();
// Stores the current maximum
// element of array []arr
int mx = arr[0];
p.Add(arr[0]);
// Iterate the array elements
for (int i = 1; i < N; i++) {
// If the current element is
// smaller than the current
// maximum, then insert
if (arr[i] < mx)
p.Add(arr[i]);
// If the current element is
// at least the current maximum
else {
p.Insert(0,arr[i]);
// Update the value of
// the current maximum
mx = arr[i];
}
}
// Print resultant permutation
foreach (int itr in p) {
Console.Write(itr + " ");
}
}
// Driver Code
public static void Main(String[] args) {
int []arr = { 3, 1, 2, 4 };
int N = arr.Length;
largestPermutation(arr, N);
}
}
// This code is contributed by Rajput-Ji
Javascript
4 3 1 2
时间复杂度: O(N)
辅助空间: O(N)