反转队列的前 K 个元素
给定一个整数 k 和一个整数队列,我们需要颠倒队列中前 k 个元素的顺序,让其他元素保持相同的相对顺序。
队列中只允许执行以下标准操作。
- enqueue(x) : 将一个项目 x 添加到队列的尾部
- dequeue() : 从队列前面删除一个项目
- size() :返回队列中元素的数量。
- front() :查找前面的项目。
例子:
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。
Input : Q = [10, 20, 30, 40, 50, 60,
70, 80, 90, 100]
k = 5
Output : Q = [50, 40, 30, 20, 10, 60,
70, 80, 90, 100]
Input : Q = [10, 20, 30, 40, 50, 60,
70, 80, 90, 100]
k = 4
Output : Q = [40, 30, 20, 10, 50, 60,
70, 80, 90, 100]
这个想法是使用辅助堆栈。
- 创建一个空堆栈。
- 将给定队列中的前 K 个项目逐一出队,并将出队的项目推入堆栈。
- 将堆栈的内容排入队列的后面
- 从前面出列(size-k)个元素,并将它们一一入队到同一个队列中。
C++
// C++ program to reverse first
// k elements of a queue.
#include
using namespace std;
/* Function to reverse the first
K elements of the Queue */
void reverseQueueFirstKElements(
int k, queue& Queue)
{
if (Queue.empty() == true
|| k > Queue.size())
return;
if (k <= 0)
return;
stack Stack;
/* Push the first K elements
into a Stack*/
for (int i = 0; i < k; i++) {
Stack.push(Queue.front());
Queue.pop();
}
/* Enqueue the contents of stack
at the back of the queue*/
while (!Stack.empty()) {
Queue.push(Stack.top());
Stack.pop();
}
/* Remove the remaining elements and
enqueue them at the end of the Queue*/
for (int i = 0; i < Queue.size() - k; i++) {
Queue.push(Queue.front());
Queue.pop();
}
}
/* Utility Function to print the Queue */
void Print(queue& Queue)
{
while (!Queue.empty()) {
cout << Queue.front() << " ";
Queue.pop();
}
}
// Driver code
int main()
{
queue Queue;
Queue.push(10);
Queue.push(20);
Queue.push(30);
Queue.push(40);
Queue.push(50);
Queue.push(60);
Queue.push(70);
Queue.push(80);
Queue.push(90);
Queue.push(100);
int k = 5;
reverseQueueFirstKElements(k, Queue);
Print(Queue);
}
Java
// Java program to reverse first k elements
// of a queue.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Reverse_k_element_queue {
static Queue queue;
// Function to reverse the first
// K elements of the Queue
static void reverseQueueFirstKElements(int k)
{
if (queue.isEmpty() == true
|| k > queue.size())
return;
if (k <= 0)
return;
Stack stack = new Stack();
// Push the first K elements into a Stack
for (int i = 0; i < k; i++) {
stack.push(queue.peek());
queue.remove();
}
// Enqueue the contents of stack
// at the back of the queue
while (!stack.empty()) {
queue.add(stack.peek());
stack.pop();
}
// Remove the remaining elements and enqueue
// them at the end of the Queue
for (int i = 0; i < queue.size() - k; i++) {
queue.add(queue.peek());
queue.remove();
}
}
// Utility Function to print the Queue
static void Print()
{
while (!queue.isEmpty()) {
System.out.print(queue.peek() + " ");
queue.remove();
}
}
// Driver code
public static void main(String args[])
{
queue = new LinkedList();
queue.add(10);
queue.add(20);
queue.add(30);
queue.add(40);
queue.add(50);
queue.add(60);
queue.add(70);
queue.add(80);
queue.add(90);
queue.add(100);
int k = 5;
reverseQueueFirstKElements(k);
Print();
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to reverse first k
# elements of a queue.
from queue import Queue
# Function to reverse the first K
# elements of the Queue
def reverseQueueFirstKElements(k, Queue):
if (Queue.empty() == True or
k > Queue.qsize()):
return
if (k <= 0):
return
Stack = []
# put the first K elements
# into a Stack
for i in range(k):
Stack.append(Queue.queue[0])
Queue.get()
# Enqueue the contents of stack
# at the back of the queue
while (len(Stack) != 0 ):
Queue.put(Stack[-1])
Stack.pop()
# Remove the remaining elements and
# enqueue them at the end of the Queue
for i in range(Queue.qsize() - k):
Queue.put(Queue.queue[0])
Queue.get()
# Utility Function to print the Queue
def Print(Queue):
while (not Queue.empty()):
print(Queue.queue[0], end =" ")
Queue.get()
# Driver code
if __name__ == '__main__':
Queue = Queue()
Queue.put(10)
Queue.put(20)
Queue.put(30)
Queue.put(40)
Queue.put(50)
Queue.put(60)
Queue.put(70)
Queue.put(80)
Queue.put(90)
Queue.put(100)
k = 5
reverseQueueFirstKElements(k, Queue)
Print(Queue)
# This code is contributed by PranchalK
C#
// C# program to reverse first k elements
// of a queue.
using System;
using System.Collections.Generic;
class GFG {
public static LinkedList queue;
// Function to reverse the first K
// elements of the Queue
public static void reverseQueueFirstKElements(int k)
{
if (queue.Count == 0 || k > queue.Count) {
return;
}
if (k <= 0) {
return;
}
Stack stack = new Stack();
// Push the first K elements into a Stack
for (int i = 0; i < k; i++) {
stack.Push(queue.First.Value);
queue.RemoveFirst();
}
// Enqueue the contents of stack at
// the back of the queue
while (stack.Count > 0) {
queue.AddLast(stack.Peek());
stack.Pop();
}
// Remove the remaining elements and
// enqueue them at the end of the Queue
for (int i = 0; i < queue.Count - k; i++) {
queue.AddLast(queue.First.Value);Complexity Analysis:
Time Complexity: O(n3).
As three nested for loops are used.
Auxiliary Space :No use of any data structure for storing values-: O(1)
queue.RemoveFirst();
}
}
// Utility Function to print the Queue
public static void Print()
{
while (queue.Count > 0) {
Console.Write(queue.First.Value + " ");
queue.RemoveFirst();
}
}
// Driver code
public static void Main(string[] args)
{
queue = new LinkedList();
queue.AddLast(10);
queue.AddLast(20);
queue.AddLast(30);
queue.AddLast(40);
queue.AddLast(50);
queue.AddLast(60);
queue.AddLast(70);
queue.AddLast(80);
queue.AddLast(90);
queue.AddLast(100);
int k = 5;
reverseQueueFirstKElements(k);
Print();
}
}
// This code is contributed by Shrikant13
输出:
50 40 30 20 10 60 70 80 90 100
复杂度分析:
- 时间复杂度: O(n+k)。
其中“n”是队列中元素的总数,“k”是要反转的元素数。这是因为首先整个队列被清空到堆栈中,然后第一个 'k' 元素被清空并以相同的方式入队。 - 辅助空间:使用堆栈来存储值以进行反转-:O(n)