Advertisement
Guest User

Untitled

a guest
May 10th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._Maximum_Element
  6. {
  7. class MaximumElement
  8. {
  9. static void Main(string[] args)
  10. {
  11. var integerN = int.Parse(Console.ReadLine());
  12.  
  13. var stack = new Stack<int>();
  14. var maxElementStack = new Stack<int>();
  15.  
  16. maxElementStack.Push(int.MinValue);
  17.  
  18. for (int count = 0; count < integerN; count++)
  19. {
  20. var querys = Console.ReadLine()
  21. .Split(' ')
  22. .Select(int.Parse)
  23. .ToArray();
  24.  
  25. int action = querys[0];
  26.  
  27. switch (action)
  28. {
  29. case 1:
  30. int elementToPush = querys[1];
  31. stack.Push(elementToPush);
  32. if(elementToPush >= maxElementStack.Peek())
  33. {
  34. maxElementStack.Push(elementToPush);
  35. }
  36.  
  37. break;
  38. case 2:
  39. int popedElement = stack.Pop();
  40. if(popedElement == maxElementStack.Peek())
  41. {
  42. maxElementStack.Pop();
  43. }
  44. break;
  45. case 3:
  46. int maxElement = maxElementStack.Peek();
  47. Console.WriteLine(maxElement);
  48. break;
  49. }
  50.  
  51. }
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement