Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Stackers {
  4.  
  5. public static Stack<Integer> stack;
  6. public static String output = "";
  7.  
  8. public static void main(String[] args) {
  9.  
  10. stack = new Stack<>();
  11. Scanner sc = new Scanner(System.in);
  12.  
  13. int n = sc.nextInt();
  14.  
  15. for(int i = 0 ; i < n + 1; i++){
  16.  
  17. String op = sc.nextLine();
  18. performOperation(op);
  19.  
  20. }
  21.  
  22. sc.close();
  23. if(!output.isEmpty()){
  24. System.out.println(output);
  25.  
  26. }
  27.  
  28. }
  29.  
  30. public static void performOperation(String op) {
  31.  
  32. if (op.startsWith("1")){
  33.  
  34. String remains = op.split(" ")[1];
  35. int rem = Integer.valueOf(remains);
  36. stack.push(rem);
  37.  
  38. }else if(op.equalsIgnoreCase("2")){
  39.  
  40. stack.pop();
  41.  
  42. }else if(op.equals("3")){
  43.  
  44. int max = (int) Collections.max(stack);
  45. output += (max + "n");
  46.  
  47. }
  48.  
  49. }
  50.  
  51. }
  52.  
  53. public class ChallengeValue {
  54. public int max;
  55. public int value;
  56. }
  57.  
  58. import java.util.*;
  59.  
  60. public class Stackers {
  61. public static Stack<ChallengeValue> stack;
  62. public static String output = "";
  63.  
  64. public static void main(String[] args) {
  65. stack = new Stack<>();
  66. Scanner sc = new Scanner(System.in);
  67.  
  68. int n = sc.nextInt();
  69.  
  70. for(int i = 0 ; i < n + 1; i++){
  71. String op = sc.nextLine();
  72. performOperation(op);
  73. }
  74.  
  75. sc.close();
  76. if(!output.isEmpty()){
  77. System.out.println(output);
  78. }
  79. }
  80.  
  81. public static void performOperation(String op) {
  82. if (op.startsWith("1")) {
  83. String remains = op.split(" ")[1];
  84. int rem = Integer.valueOf(remains);
  85.  
  86. ChallengeValue newVal = new ChallengeValue();
  87. newVal.value = rem;
  88.  
  89. ChallengeValue last = stack.peek();
  90. newVal.max = last.max > newVal.value ? last.max : newVal.max;
  91. stack.push(newVal);
  92. } else if(op.equalsIgnoreCase("2")) {
  93. stack.pop();
  94. } else if(op.equals("3")) {
  95. int max = stack.peek().max;
  96. output += (max + "n");
  97. }
  98. }
  99. }
  100.  
  101. public class Stacker {
  102.  
  103. private final Deque<Integer> stack = new ArrayDeque<>();
  104.  
  105. public Optional<Integer> processOperation(String op) {
  106. char opc = op.charAt(0);
  107. switch(opc) {
  108. case '1':
  109. int previous = stack.isEmpty() ? Integer.MIN_VALUE : stack.peek();
  110. int max = Math.max(previous, Integer.parseInt(op.substring(1).trim()));
  111. stack.push(max);
  112. return Optional.empty();
  113. case '2':
  114. if (stack.isEmpty()) {
  115. throw new IllegalStateException("Cannot pop empty stack");
  116. }
  117. stack.pop();
  118. return Optional.empty();
  119. case '3':
  120. if (stack.isEmpty()) {
  121. throw new IllegalStateException("Cannot max empty stack");
  122. }
  123. return Optional.of(stack.peek());
  124. default:
  125. throw new IllegalStateException("Unknown operation:" + op);
  126. }
  127. }
  128. }
  129.  
  130. Stacker stacker = new Stacker();
  131. String report = commands.map(stacker::processOperation)
  132. .filter(Optional::isPresent)
  133. .map(opt -> String.valueOf(opt.get()))
  134. .collect(Collectors.joining("n"));
  135.  
  136. scanner.useDelimiter("\s*\n");
  137. int count = Integer.parseInt(scanner.next());
  138. // convert STDIN lines to a stream
  139. Stream<String> commands = StreamSupport.stream(Spliterators.spliterator(scanner, count, Spliterator.ORDERED), false);
  140.  
  141. import java.io.StringReader;
  142. import java.util.ArrayDeque;
  143. import java.util.Deque;
  144. import java.util.Optional;
  145. import java.util.Scanner;
  146. import java.util.Spliterator;
  147. import java.util.Spliterators;
  148. import java.util.stream.Collectors;
  149. import java.util.stream.Stream;
  150. import java.util.stream.StreamSupport;
  151.  
  152. public class Stacker {
  153.  
  154. private final Deque<Integer> stack = new ArrayDeque<>();
  155.  
  156. public Optional<Integer> processOperation(String op) {
  157. char opc = op.charAt(0);
  158. switch(opc) {
  159. case '1':
  160. int previous = stack.isEmpty() ? Integer.MIN_VALUE : stack.peek();
  161. int max = Math.max(previous, Integer.parseInt(op.substring(1).trim()));
  162. stack.push(max);
  163. return Optional.empty();
  164. case '2':
  165. if (stack.isEmpty()) {
  166. throw new IllegalStateException("Cannot pop empty stack");
  167. }
  168. stack.pop();
  169. return Optional.empty();
  170. case '3':
  171. if (stack.isEmpty()) {
  172. throw new IllegalStateException("Cannot max empty stack");
  173. }
  174. return Optional.of(stack.peek());
  175. default:
  176. throw new IllegalStateException("Unknown operation:" + op);
  177. }
  178. }
  179.  
  180.  
  181.  
  182. public static void main(String[] args) {
  183. String testdata = "10n" +
  184. "1 97n" +
  185. "2n" +
  186. "1 20n" +
  187. "2n" +
  188. "1 26n" +
  189. "1 20n" +
  190. "2n" +
  191. "3n" +
  192. "1 91n" +
  193. "3";
  194.  
  195. try (Scanner scanner = new Scanner(new StringReader(testdata))) {
  196. scanner.useDelimiter("\s*\n");
  197. int count = Integer.parseInt(scanner.next());
  198. // convert STDIN lines to a stream
  199. Stream<String> commands = StreamSupport.stream(Spliterators.spliterator(scanner, count, Spliterator.ORDERED), false);
  200. // process the operations:
  201. Stacker stacker = new Stacker();
  202. String report = commands.map(stacker::processOperation)
  203. .filter(Optional::isPresent)
  204. .map(opt -> String.valueOf(opt.get()))
  205. .collect(Collectors.joining("n"));
  206. System.out.println(report);
  207. }
  208. }
  209.  
  210. }
  211.  
  212. public class Solution {
  213.  
  214. public static void main(String[] args) {
  215.  
  216. Scanner scan = new Scanner(System.in);
  217. Stack<Integer> stacky = new Stack<Integer>();
  218. int N = scan.nextInt();
  219. int ch;
  220. int x,ele=0;
  221. while(N-- >0){
  222. ch = scan.nextInt();
  223. if(ch==1){x = scan.nextInt();
  224. if(stacky.isEmpty()){
  225. stacky.push(x);
  226. }
  227. else{
  228. int y = stacky.peek();
  229. stacky.push(Math.max(x, y));
  230. }
  231. }
  232.  
  233. if(ch==2){
  234. if(!stacky.isEmpty())
  235. stacky.pop();
  236. }
  237.  
  238. if(ch==3){
  239. System.out.println(stacky.peek());
  240. }
  241. }}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement