알고리즘

BaseBall Game(leetcode 682)

뮹뭉묵목몽묭 2019. 7. 28. 23:08

https://leetcode.com/problems/baseball-game/

 

Baseball Game - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

You're now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round's score): Directly represents the number of points you get in this round.
  2. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
  3. "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
  4. "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.

Each round's operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

Example 1:

Input: ["5","2","C","D","+"] Output: 30 Explanation: Round 1: You could get 5 points. The sum is: 5. Round 2: You could get 2 points. The sum is: 7. Operation 1: The round 2's data was invalid. The sum is: 5. Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15. Round 4: You could get 5 + 10 = 15 points. The sum is: 30.

Example 2:

Input: ["5","-2","4","C","D","9","+","+"] Output: 27 Explanation: Round 1: You could get 5 points. The sum is: 5. Round 2: You could get -2 points. The sum is: 3. Round 3: You could get 4 points. The sum is: 7. Operation 1: The round 3's data is invalid. The sum is: 3. Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1. Round 5: You could get 9 points. The sum is: 8. Round 6: You could get -4 + 9 = 5 points. The sum is 13. Round 7: You could get 9 + 5 = 14 points. The sum is 27.

 

배열이 주어지고 배열안의 모든 값을 ouput으로 내는 문제이다. 하지만 특수한 경우들이있다.

1. "+" 문자가 있는경우 전 두값을 더한 값을 

2. "D" 문자가 있는경우 전 값의 두배를

3. "C" 문자가 있는경우 전 값을 삭제

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
    public int calPoints(String[] ops) {
        Stack<Integer> stack = new Stack<>();
        for (String str : ops) {
            //배열에 문자가 있을경우
            if(str.equals("C")||str.equals("D")||str.equals("+")) {
                switch(str){
                        //문자가 C일 경우 C의전 숫자 값을 없어져야 하므로 pop()을 해서 삭제한다
                    case "C": stack.pop();
                        break;
                        //문자가 D일 경우 D의 전숫자의 두배의 값을 넣어야한다.
                    case "D" : stack.push(stack.peek()*2);
                        break;
                        //문자가 +일 경우 +의 전숫자 +의 전전숫자를 합한 값을 넣어야하므로
                        //전전 숫자의 값을 꺼낼려면 전숫자의 값을 임시로 저장한다. peek()을
                        //통해 전전 숫자값을 임시저장한다.
                        //전 숫자값을 push 하고 전+전전숫자의 값을 넣어준다.
                    case "+" : int second = stack.pop();
                                int first = stack.peek();
                                stack.push(second);
                        stack.push(first+second);
                        break;
                        
                        
                }
                //숫자일경우 해당 숫자값을 스택에 push
            } else {
                stack.push(Integer.valueOf(str));
            }
            
        }
        //스택의 모든값을 pop시키면서 sum에 더해줌
        int sum =0;
        while (!stack.isEmpty()) {
            sum += stack.pop();
        }
        //sum을 리턴
        return sum;
        
    }
}
cs