iLLcheff

Baseball Game

Feb 10th, 2023 (edited)
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. import static java.lang.Character.isDigit;
  5. import static java.lang.Character.isLetter;
  6.  
  7. public class BaseballGame {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         Queue<String> queue = new LinkedList<>();
  11.         Stack<Integer> stack = new Stack<>();
  12.         String input = scanner.nextLine();
  13.  
  14.         for (int i = 0; i < input.length(); i++) {
  15.             if(input.charAt(i) == '-'){
  16.                 stack.add(-input.charAt(i+1));
  17.                 continue;
  18.             }
  19.             if (isDigit(input.charAt(i))) {
  20.                 stack.add(Character.getNumericValue(input.charAt(i)));
  21.             }
  22.             if(isLetter(input.charAt(i))){
  23.                 if(input.charAt(i) == 'C' || input.charAt(i) == 'D'){
  24.                     queue.offer(Character.toString(input.charAt(i)));
  25.                 }
  26.             }
  27.             if(input.charAt(i) == '+'){
  28.                 queue.offer(Character.toString(input.charAt(i)));
  29.             }
  30.         }
  31.         while(!queue.isEmpty()) {
  32.             int result = 0;
  33.             if (queue.peek().equals("C")) {
  34.                 stack.pop();
  35.                 queue.poll();
  36.             }
  37.             if (queue.peek().equals("D")) {
  38.                 result = stack.peek() * 2;
  39.                 stack.push(result);
  40.                 queue.poll();
  41.             }
  42.             if (queue.peek().equals("+")) {
  43.                 int temp1 = stack.pop();
  44.                 int temp2 = stack.pop();
  45.                 result = temp1 + temp2;
  46.                 stack.push(temp2);
  47.                 stack.push(temp1);
  48.                 stack.push(result);
  49.                 queue.poll();
  50.             }
  51.         }
  52.         int total = 0;
  53.         for (int n : stack) {
  54.             total += n;
  55.         }
  56.         System.out.println(total);
  57.  
  58.     }
  59. }
  60.  
  61.  
  62. // TODO
  63. //        EXAMPLE 1:
  64. //        Input: ops = ["5","2","C","D","+"]
  65. //        Output: 30
  66. //        Explanation:
  67. //        "5" - Add 5 to the record, record is now [5].
  68. //        "2" - Add 2 to the record, record is now [5, 2].
  69. //        "C" - Invalidate and remove the previous score, record is now [5].
  70. //        "D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
  71. //        "+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
  72. //        The total sum is 5 + 10 + 15 = 30.
  73. //        EXAMPLE 2:
  74. //        Input: ops = ["5","-2","4","C","D","9","+","+"]
  75. //        Output: 27
  76. //        Explanation:
  77. //        "5" - Add 5 to the record, record is now [5].
  78. //        "-2" - Add -2 to the record, record is now [5, -2].
  79. //        "4" - Add 4 to the record, record is now [5, -2, 4].
  80. //        "C" - Invalidate and remove the previous score, record is now [5, -2].
  81. //        "D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
  82. //        "9" - Add 9 to the record, record is now [5, -2, -4, 9].
  83. //        "+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
  84. //        "+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
  85. //        The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
  86. //        EXAMPLE 3:
  87. //        Input: ops = ["1","C"]
  88. //        Output: 0
  89. //        Explanation:
  90. //        "1" - Add 1 to the record, record is now [1].
  91. //        "C" - Invalidate and remove the previous score, record is now [].
  92. //        Since the record is empty, the total sum is 0.
  93. //        Constraints:
  94. //        1 <= operations.length <= 1000
  95. //        operations[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
  96. //        For operation "+", there will always be at least two previous scores on the record.
  97. //        For operations "C" and "D", there will always be at least one previous score on the record.
  98. //
Advertisement
Add Comment
Please, Sign In to add comment