给定大小为N的数组arr[]和查询Q[][] ,任务是对给定数组执行以下类型的查询。 0:将数组左移一位。
- 1:将数组右移一位。
- 2 XY:更新arr[X] = Y 的值。
- 3 X:打印arr[X] 。
例子:
Input: arr[]={1, 2, 3, 4, 5}, Q[][]={{0}, {1}, {3, 1}, {2, 2, 54}, {3, 2}}.
Output:4 54
Explanation:
Query1: The array arr[] modifies to {2, 3, 4, 5, 1}
Query2: The array arr[] modifies to {1, 2, 3, 4, 5}
Query3: Print the value of arr[1] i.e. 2
Query4: The array arr[] modifies to {1, 54, 3, 4, 5}
Query5: Print the value of arr[2], i.e. 54.
Input: arr[]={1}, Q[][]={{0}, {1}, {2, 0, 54}, {3, 0}}
Output: 54
处理方法:使用Deque(Double Ended queue)在O(1)中进行队列前后的插入和删除操作即可解决问题。请按照以下步骤解决问题。
- 创建一个双端队列dq 。
- 将数组arr[] 的所有元素推送到dq 。
- 对于类型0 (左移)的查询,从dq的前面弹出一个元素并将该元素推到dq的后面。
- 对于类型1(右移)的查询,从dq的后面弹出一个元素并将该元素推到dq 的前面。
- 对于类型2的查询,更新dq[X] = Y 。
- 对于类型3的查询,打印dq[X] 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to perform the
// given operations
void Queries(int arr[], int N,
vector >& Q)
{
// Dequeue to store the
// array elements
deque dq;
// Insert all element of
// the array into the dequeue
for (int i = 0; i < N; i++) {
dq.push_back(arr[i]);
}
// Stores the size of the queue
int sz = Q.size();
// Traverse each query
for (int i = 0; i < sz; i++) {
// Query for left shift.
if (Q[i][0] == 0) {
// Extract the element at
// the front of the queue
int front = dq[0];
// Pop the element at
// the front of the queue
dq.pop_front();
// Push the element at
// the back of the queue
dq.push_back(front);
}
// Query for right shift
else if (Q[i][0] == 1) {
// Extract the element at
// the back of the queue
int back = dq[N - 1];
// Pop the element at
// the back of the queue
dq.pop_back();
// Push the element at
// the front of the queue
dq.push_front(back);
}
// Query for update
else if (Q[i][0] == 2) {
dq[Q[i][1]] = Q[i][2];
}
// Query to get the value
else {
cout << dq[Q[i][1]] << " ";
}
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
vector > Q;
// All possible Queries
Q = { { 0 }, { 1 }, { 3, 1 },
{ 2, 2, 54 }, { 3, 2 } };
Queries(arr, N, Q);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to perform the
// given operations
static void Queries(int arr[], int N,
int [][]Q)
{
// Dequeue to store the
// array elements
Vector dq = new Vector<>();
// Insert all element of
// the array into the dequeue
for (int i = 0; i < N; i++)
{
dq.add(arr[i]);
}
// Stores the size of the queue
int sz = Q.length;
// Traverse each query
for (int i = 0; i < sz; i++)
{
// Query for left shift.
if (Q[i][0] == 0)
{
// Extract the element at
// the front of the queue
int front = dq.get(0);
// Pop the element at
// the front of the queue
dq.remove(0);
// Push the element at
// the back of the queue
dq.add(front);
}
// Query for right shift
else if (Q[i][0] == 1)
{
// Extract the element at
// the back of the queue
int back = dq.elementAt(dq.size() - 1);
// Pop the element at
// the back of the queue
dq.remove(dq.size() - 1);
// Push the element at
// the front of the queue
dq.add(0, back);
}
// Query for update
else if (Q[i][0] == 2)
{
dq.set(Q[i][1], Q[i][2]);
}
// Query to get the value
else
{
System.out.print(dq.get(Q[i][1]) + " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = {1, 2, 3, 4, 5};
int N = arr.length;
// Vector
// > Q = new Vector<>();
// All possible Queries
int [][]Q = {{0}, {1}, {3, 1},
{2, 2, 54}, {3, 2}};
Queries(arr, N, Q);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program to implement
# the above approach
from collections import deque
# Function to perform the
# given operations
def Queries(arr, N, Q):
# Dequeue to store the
# array elements
dq = deque()
# Insert all element of
# the array into the dequeue
for i in range(N):
dq.append(arr[i])
# Stores the size of the queue
sz = len(Q)
# Traverse each query
for i in range(sz):
# Query for left shift.
if (Q[i][0] == 0):
# Extract the element at
# the front of the queue
front = dq[0]
# Pop the element at
# the front of the queue
dq.popleft()
# Push the element at
# the back of the queue
dq.appendleft(front)
# Query for right shift
elif (Q[i][0] == 1):
# Extract the element at
# the back of the queue
back = dq[N - 1]
# Pop the element at
# the back of the queue
dq.popleft()
# Push the element at
# the front of the queue
dq.appendleft(back)
# Query for update
elif (Q[i][0] == 2):
dq[Q[i][1]] = Q[i][2]
# Query to get the value
else:
print(dq[Q[i][1]], end = " ")
# Driver Code
if __name__ == '__main__':
arr = [ 1, 2, 3, 4, 5 ]
N = len(arr)
# All possible Queries
Q = [ [ 0 ], [ 1 ], [ 3, 1 ],
[ 2, 2, 54 ], [ 3, 2 ] ]
Queries(arr, N, Q)
# This code is contributed by mohit kumar 29
输出:
2 54
时间复杂度:O(N+|Q|)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live