您将获得3个堆栈,A(输入堆栈)、B(辅助堆栈)和C(输出堆栈)。最初,堆栈A包含从1到N的数字,您需要按排序顺序将所有数字从堆栈A传输到堆栈C,即最后,堆栈C的底部应该有最小的元素,顶部应该有最大的元素。您可以使用堆栈B,也就是说,您可以随时将元素推/弹出到堆栈B。在堆栈A的末尾,B应该是空的。
null
例如:
输入: A={4,3,1,2,5} 输出: 是的7
输入: A={3,4,1,2,5} 输出: 不
方法: 从给定堆栈的底部迭代。初始化 必修的 作为stackC中最底部的元素,位于末尾,即1。按照下面给出的算法来解决上述问题。
- 如果堆栈元素等于所需元素,则传输次数将是从A传输到C的计数。
- 如果它不等于所需的元素,则通过将其与堆栈中最顶层的元素进行比较来检查是否可以传输它。
- 如果stackC中最顶端的元素大于stackA[i]元素,则无法以排序方式传输它,
- 否则,将元素推到stackC并递增传输。
- 在stackC中迭代并弹出最上面的元素,直到它等于required和increment required,并在每个步骤中转移。
以下是上述方法的实施情况:
C++
// C++ program for // Sudo Placement | playing with stacks #include <bits/stdc++.h> using namespace std; // Function to check if it is possible // count the number of steps void countSteps( int sa[], int n) { // Another stack stack< int > 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<Integer> sc = new Stack<Integer>(); // 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< int > sc = new Stack< int >(); // 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
<script> // Javascript program for Sudo // Placement | playing with stacks // Function to check if it is possible // count the number of steps function countSteps(sa, n) { // Another stack let sc = []; // variables to count transfers let required = 1, transfer = 0; // iterate in the stack in reverse order for (let 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.length!=0 && sc[sc.length-1] < sa[i]) { document.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.length!=0 && sc[sc.length-1] == required) { required++; sc.pop(); transfer++; } } // print the steps document.write( "YES " + transfer+ "<br>" ); } // Driver Code let sa=[4, 3, 1, 2, 5]; let n = sa.length; countSteps(sa, n); // This code is contributed by rag2127 </script> |
输出:
YES 7
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END