须藤放置[1.3] |玩堆栈
您有 3 个堆栈,A(输入堆栈)、B(辅助堆栈)和 C(输出堆栈)。最初堆栈 A 包含从 1 到 N 的数字,您需要将堆栈 A 中的所有数字按排序顺序传输到堆栈 C,即最后,堆栈 C 应该在底部具有最小元素,在顶部具有最大元素。您可以随时使用堆栈 B,即您也可以随时将元素推送/弹出到堆栈 B。在最后的堆栈 A,B 应该是空的。
例子:
Input: A = {4, 3, 1, 2, 5}
Output: Yes 7
Input: A = {3, 4, 1, 2, 5}
Output: No
方法:从给定堆栈的底部迭代。将required初始化为最后的 stackC 中最底部的元素,即 1. 按照下面给出的算法解决上述问题。
- 如果堆栈元素等于所需元素,则传输次数将为 1,即从 A 传输到 C 的次数。
- 如果它不等于所需元素,则通过将其与堆栈中的最顶部元素进行比较来检查是否可以传输它。
- 如果 stackC 中最顶层的元素大于 stackA[i] 元素,那么就不能以排序的方式传递它,
- 否则将元素推送到 stackC 并递增传输。
- 在 stackC 中迭代并弹出最顶部的元素,直到它等于所需的和所需的增量,并在每个步骤中传输。
下面是上述方法的实现:
C++
// C++ program for
// Sudo Placement | playing with stacks
#include
using namespace std;
// Function to check if it is possible
// count the number of steps
void countSteps(int sa[], int n)
{
// Another stack
stack sc;
// variables to count transfers
int required = 1, transfer = 0;
// iterate in the stack in reverse order
for (int i = 0; i < n; i++) {
// if the last element has to be
// inserted by removing elements
// then count the number of steps
if (sa[i] == required) {
required++;
transfer++;
}
else {
// if stack is not empty and top element
// is smaller than current element
if (!sc.empty() && sc.top() < sa[i]) {
cout << "NO";
return;
}
// push into stack and count operation
else {
sc.push(sa[i]);
transfer++;
}
}
// stack not empty, then pop the top element
// pop out all elements till is it equal to required
while (!sc.empty() && sc.top() == required) {
required++;
sc.pop();
transfer++;
}
}
// print the steps
cout << "YES " << transfer;
}
// Driver Code
int main()
{
int sa[] = { 4, 3, 1, 2, 5 };
int n = sizeof(sa) / sizeof(sa[0]);
countSteps(sa, n);
return 0;
}
Java
// Java program for Sudo
// Placement | playing with stacks
import java.util.*;
class GFG
{
// Function to check if it is possible
// count the number of steps
static void countSteps(int sa[], int n)
{
// Another stack
Stack sc = new Stack();
// variables to count transfers
int required = 1, transfer = 0;
// iterate in the stack in reverse order
for (int i = 0; i < n; i++)
{
// if the last element has to be
// inserted by removing elements
// then count the number of steps
if (sa[i] == required)
{
required++;
transfer++;
}
else
// if stack is not empty and top element
// is smaller than current element
if (!sc.empty() && sc.peek() < sa[i])
{
System.out.print("NO");
return;
}
// push into stack and count operation
else
{
sc.push(sa[i]);
transfer++;
}
// stack not empty, then pop the top element
// pop out all elements till is it equal to required
while (!sc.empty() && sc.peek() == required)
{
required++;
sc.pop();
transfer++;
}
}
// print the steps
System.out.println("YES " + transfer);
}
// Driver Code
public static void main(String[] args)
{
int sa[] = {4, 3, 1, 2, 5};
int n = sa.length;
countSteps(sa, n);
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 program for
# Sudo Placement | playing with stacks
from typing import List
# Function to check if it is possible
# count the number of steps
def countSteps(sa: List[int], n: int) -> None:
# Another stack
sc = []
# Variables to count transfers
required = 1
transfer = 0
# Iterate in the stack in reverse order
for i in range(n):
# If the last element has to be
# inserted by removing elements
# then count the number of steps
if (sa[i] == required):
required += 1
transfer += 1
else:
# If stack is not empty and top element
# is smaller than current element
if (sc and sc[-1] < sa[i]):
print("NO")
return
# push into stack and count operation
else:
sc.append(sa[i])
transfer += 1
# stack not empty, then pop the top
# element pop out all elements till
# is it equal to required
while (sc and sc[-1] == required):
required += 1
sc.pop()
transfer += 1
# Print the steps
print("YES {}".format(transfer))
# Driver Code
if __name__ == "__main__":
sa = [ 4, 3, 1, 2, 5 ]
n = len(sa)
countSteps(sa, n)
# This code is contributed by sanjeev2552
C#
// C# program for Sudo
// Placement | playing with stacks
using System;
using System.Collections.Generic;
public class GFG
{
// Function to check if it is possible
// count the number of steps
static void countSteps(int []sa, int n)
{
// Another stack
Stack sc = new Stack();
// variables to count transfers
int required = 1, transfer = 0;
// iterate in the stack in reverse order
for (int i = 0; i < n; i++)
{
// if the last element has to be
// inserted by removing elements
// then count the number of steps
if (sa[i] == required)
{
required++;
transfer++;
}
else
// if stack is not empty and top element
// is smaller than current element
if (sc.Count!=0 && sc.Peek() < sa[i])
{
Console.Write("NO");
return;
}
// push into stack and count operation
else
{
sc.Push(sa[i]);
transfer++;
}
// stack not empty, then pop the top element
// pop out all elements till is it equal to required
while (sc.Count!=0 && sc.Peek() == required)
{
required++;
sc.Pop();
transfer++;
}
}
// print the steps
Console.WriteLine("YES " + transfer);
}
// Driver Code
public static void Main(String[] args)
{
int []sa = {4, 3, 1, 2, 5};
int n = sa.Length;
countSteps(sa, n);
}
}
// This code has been contributed by 29AjayKumar
Javascript
输出:
YES 7