给定两个数组arr[]和operations[] ,由N和M 个整数组成,以及一个整数C 。数组arr[]表示N 个盒子中的初始数据包数量,每个盒子的容量为C 。从位置1开始,按照数组operations[]指定的顺序执行以下类型的操作:
- 类型 1:移至左侧框。
- 类型 2:移至右侧框。
- 类型 3:从当前框中选择一个数据包。
- 类型 4:从当前盒子中丢弃一个数据包。
- 类型5:打印每个框中的数据包数量并退出。
在以下情况下,任何操作都将被忽略:
- 如果当前位置为1 ,则忽略左移操作。
- 如果当前位置为N ,则忽略右移操作。
- 如果一个数据包已经被拾取并且还没有被丢弃或者当前框是空的,那么拾取一个数据包操作将被忽略。
- 如果没有数据包被拾取或当前盒子已经有C 个数据包,则丢弃数据包操作被忽略
例子:
Input: N = 3, C = 5, arr[] = {2, 5, 2}, M = 6, operations[] = {3, 2, 4, 1, 4, 5}
Output: {1, 5, 2}
Explanation:
Operations can be performed as follows starting from position 1:
Type 3: Pick a packet. Position = 1 and arr[] = {1, 5, 2}.
Type 2: Move right. Position = 2 and arr[] = {1, 5, 2}
Type 4: Drop packet. Position = 2 and arr[] = {1, 5, 2}
Type 1: Move left. Position = 1 and arr[] = {1, 5, 2}.
Type 4: Drop packet. Position = 1 and arr[] = {2, 5, 2}.
Type 5: Print array arr[] = {2, 5, 2}.
Input: N = 3, C = 1, arr[] = {1, 1, 1}, M = 4, operations[] = {3, 2, 4, 5}
Output: {0, 1, 1}
Explanation:
Operations can be performed as follows starting from position 1:
Type 3: Pick a packet. Position = 1 and arr[] = {0, 1, 1}.
Type 2: Move right. Position = 2 and arr[] = {0, 1, 1}
Type 4: Drop packet. Position = 2 and arr[] = {0, 1, 1}
Type 5: Print array arr[] = {0, 1, 1}.
方法:想法是为每个任务分配变量,以便对于当前索引,使用0初始化curr 。要检查元素是否被选取,请使用false初始化选取的元素。请按照以下步骤解决问题:
- 请拿起跟踪,如果一些数据包已经选择了一个布尔变量。
- 如果操作类型为1且curr不为0 ,则向左移动,将curr递减1 。
- 如果操作类型为2且curr不是(N – 1) ,则向右移动,将当前curr增加1 。
- 如果操作类型为3且picked为false且当前box 不为空,则将当前box 中的数据包减1 ,并将picked标记为true 。
- 如果操作类型为4且picked为true且当前box 未满,则将当前box 中的packet 增加1并将picked标记为false 。
- 如果操作类型为5 ,则打印数组arr[] 中的当前值并退出。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define ll long long int
// Function to print final array after
// performing all the operations
int printFinalArray(int* a, int n,
int* operations,
int p, int capacity)
{
// Initialize variables
int i, curr = 0;
bool picked = false;
// Traverse through all operations
for (i = 0; i < p; i++) {
// Operation Type
int s = operations[i];
bool flag = false;
switch (s) {
// Move left
case 1:
if (curr != 0)
curr--;
break;
// Move right
case 2:
if (curr != n - 1)
curr++;
break;
// Pick a packet
case 3:
if (picked == false
&& a[curr] != 0) {
picked = true;
a[curr]--;
}
break;
// Drop a packet
case 4:
if (picked == true
&& a[curr] != capacity) {
picked = false;
a[curr]++;
}
break;
// Exit
default:
flag = true;
}
if (flag == true)
break;
}
// Print final array
for (i = 0; i < n; i++) {
cout << a[i] << " ";
}
}
// Driver Code
int main()
{
// Given capacity
int capacity = 5;
// Given array with initial values
int a[] = { 2, 5, 2 };
// Array size
int N = sizeof(a) / sizeof(a[0]);
// Operations
int operations[] = { 3, 2, 4, 1, 4, 5 };
// Number of operations
int M = sizeof(operations)
/ sizeof(operations[0]);
// Function call
printFinalArray(a, N, operations,
M, capacity);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print final array after
// performing aint the operations
static void printFinalArray(int []a, int n,
int []operations,
int p, int capacity)
{
// Initialize variables
int i, curr = 0;
boolean picked = false;
// Traverse through aint operations
for(i = 0; i < p; i++)
{
// Operation Type
int s = operations[i];
boolean flag = false;
switch(s)
{
// Move left
case 1:
if (curr != 0)
curr--;
break;
// Move right
case 2:
if (curr != n - 1)
curr++;
break;
// Pick a packet
case 3:
if (picked == false &&
a[curr] != 0)
{
picked = true;
a[curr]--;
}
break;
// Drop a packet
case 4:
if (picked == true &&
a[curr] != capacity)
{
picked = false;
a[curr]++;
}
break;
// Exit
default:
flag = true;
}
if (flag == true)
break;
}
// Print final array
for(i = 0; i < n; i++)
{
System.out.print(a[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Given capacity
int capacity = 5;
// Given array with initial values
int a[] = { 2, 5, 2 };
// Array size
int N = a.length;
// Operations
int operations[] = { 3, 2, 4, 1, 4, 5 };
// Number of operations
int M = operations.length;
// Function call
printFinalArray(a, N, operations,
M, capacity);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to print final array after
# performing all the operations
def printFinalArray(a, n,
operations, p, capacity):
# Initialize variables
curr = 0
picked = False
# Traverse through all operations
for i in range(p):
# Operation Type
s = operations[i]
flag = False
# Move left
if (curr != 0):
curr -= 1
break
# Move right
if (curr != n - 1):
curr += 1
break
# Pick a packet
if (picked == False
and a[curr] != 0):
picked = True
a[curr] -= 1
break
# Drop a packet
if (picked == True
and a[curr] != capacity):
picked = False
a[curr] += 1
break
# Exit
else:
flag = True
if (flag == True):
break
# Print final array
for i in range(n):
print(a[i], end=" ")
# Driver Code
if __name__ == "__main__":
# Given capacity
capacity = 5
# Given array with initial values
a = [2, 5, 2]
# Array size
N = len(a)
# Operations
operations = [3, 2, 4, 1, 4, 5]
# Number of operations
M = len(operations)
# Function call
printFinalArray(a, N, operations,
M, capacity)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG{
// Function to print final array after
// performing aint the operations
static void printFinalArray(int []a, int n,
int []operations,
int p, int capacity)
{
// Initialize variables
int i, curr = 0;
bool picked = false;
// Traverse through aint operations
for(i = 0; i < p; i++)
{
// Operation Type
int s = operations[i];
bool flag = false;
switch(s)
{
// Move left
case 1:
if (curr != 0)
curr--;
break;
// Move right
case 2:
if (curr != n - 1)
curr++;
break;
// Pick a packet
case 3:
if (picked == false &&
a[curr] != 0)
{
picked = true;
a[curr]--;
}
break;
// Drop a packet
case 4:
if (picked == true &&
a[curr] != capacity)
{
picked = false;
a[curr]++;
}
break;
// Exit
default:
flag = true;
break;
}
if (flag == true)
break;
}
// Print final array
for(i = 0; i < n; i++)
{
Console.Write(a[i] + " ");
}
}
// Driver Code
public static void Main()
{
// Given capacity
int capacity = 5;
// Given array with initial values
int[] a = { 2, 5, 2 };
// Array size
int N = a.Length;
// Operations
int[] operations = { 3, 2, 4, 1, 4, 5 };
// Number of operations
int M = operations.Length;
// Function call
printFinalArray(a, N, operations,
M, capacity);
}
}
// This code is contributed by sanjoy_62
Javascript
2 5 2
时间复杂度: O(M + N)
辅助空间: O(M + N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live