We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Spent ages trying to find input from args when it actually comes from Console.ReadLine. Had to read up on two stacks before attempting question.
Was failing a few test cases on time limit until I switched from using linq .Any() to checking .Count > 0
classSolution{staticvoidMain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */(Stack<int>stack1,Stack<int>stack2)=(new(),new());varnumberOfInputs=Int32.Parse(Console.ReadLine()!);for(int_=0;_<numberOfInputs;_++){vararg=Console.ReadLine()!.Split();switch(arg[0]){case"1":varenqueueValue=Int32.Parse(arg[1]);stack1.Push(enqueueValue);continue;case"2":case"3":if(stack2.Count==0){while(stack1.Count>0){stack2.Push(stack1.Pop());}}if(arg[0]=="3"){Console.WriteLine(stack2.Count>0?stack2.Peek():stack1.Peek());continue;}stack2.Pop();continue;default:thrownewArgumentException("Ffs Hackerrank");}}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
My C# solution
Spent ages trying to find input from args when it actually comes from Console.ReadLine. Had to read up on two stacks before attempting question. Was failing a few test cases on time limit until I switched from using linq .Any() to checking .Count > 0