检查堆栈或队列中的移动是否可能
给定一个二进制数组,其中 1 表示推送操作,0 表示堆栈或队列中的弹出操作。任务是检查可能的操作集是否有效。
例子:
Input: a[] = {1, 1, 0, 0, 1}
Output: Valid
Input : a[] = {1, 1, 0, 0, 0}
Output : Invalid
The third pop operation cannot be done as the stack or queue will be empty at that moment of time.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
一种幼稚的做法是使用堆栈或队列,当数组元素为1时执行入栈操作,当数组元素为0时执行出栈操作。当有出栈操作时,则移动无效如果堆栈或队列为空。如果我们可以执行所有操作,那么这些动作是有效的。
时间复杂度:O(N)
辅助空间:O(N)
一种有效的方法是计算推送操作并在执行弹出操作时减少它们。如果在任何实例期间,计数变为小于 0,则操作集无效。
下面是上述方法的实现。
C++
// C++ program to Check if moves in a stack
// or queue are possible or not
#include
using namespace std;
// Function to check if
// operations are valid or not
bool check(int a[], int n)
{
// count number of push operations
int ones = 0;
// traverse in the array
for (int i = 0; i < n; i++) {
// push operation
if (a[i])
ones++;
// pop operation
else
ones--;
// if at any moment pop() operations
// exceeds the number of push operations
if (ones < 0)
return false;
}
return true;
}
// Driver Code
int main()
{
int a[] = { 1, 1, 0, 0, 1 };
int n = sizeof(a) / sizeof(a[0]);
if (check(a, n))
cout << "Valid";
else
cout << "Invalid";
}
Java
// Java program to Check if moves in a stack
// or queue are possible or not
public class GFG {
// Function to check if
// operations are valid or not
static boolean check(int a[], int n) {
// count number of push operations
int ones = 0;
// traverse in the array
for (int i = 0; i < n; i++) {
// push operation
if (a[i] ==1) {
ones++;
} // pop operation
else {
ones--;
}
// if at any moment pop() operations
// exceeds the number of push operations
if (ones < 0) {
return false;
}
}
return true;
}
// Driver Code
static public void main(String[] args) {
int a[] = {1, 1, 0, 0, 1};
int n = a.length;
if (check(a, n)) {
System.out.println("Valid");
} else {
System.out.println("Invalid");
}
}
}
// This code is contributed by Rajput-Ji
Python 3
# Python 3 program to Check if moves
# in a stack or queue are possible or not
# Function to check if
# operations are valid or not
def check(a, n):
# count number of push operations
ones = 0;
# traverse in the array
for i in range (0, n):
# push operation
if (a[i]):
ones = ones + 1;
# pop operation
else:
ones = ones - 1;
# if at any moment pop() operations
# exceeds the number of push operations
if (ones < 0):
return False;
return True;
# Driver Code
a = [ 1, 1, 0, 0, 1 ];
n = len(a);
if (check(a, n)):
print("Valid");
else:
print("Invalid");
# This code is contributed
# by Akanksha Rai
C#
using System;
// C# program to Check if moves in a stack
// or queue are possible or not
public class GFG {
// Function to check if
// operations are valid or not
static bool check(int []a, int n) {
// count number of push operations
int ones = 0;
// traverse in the array
for (int i = 0; i < n; i++) {
// push operation
if (a[i] ==1) {
ones++;
} // pop operation
else {
ones--;
}
// if at any moment pop() operations
// exceeds the number of push operations
if (ones < 0) {
return false;
}
}
return true;
}
// Driver Code
static public void Main() {
int []a = {1, 1, 0, 0, 1};
int n = a.Length;
if (check(a, n)) {
Console.Write("Valid");
} else {
Console.Write("Invalid");
}
}
}
// This code is contributed by Rajput-Ji
PHP
Javascript
输出:
Valid
时间复杂度:O(N)
辅助空间:O(1)