✅🔥 BASEBALL GAME 🔥 || 💡 OPTIMIZED C++ CODE ✅ || 🌟 BEATS 100% || 🧑‍💻 BEGINNER-FRIENDLY
IntuitionThe problem involves processing operations on a scoreboard. Using a stack is an intuitive choice because it allows us to efficiently manage the current


# Intuition
The problem involves processing operations on a scoreboard. Using a **stack** is an intuitive choice because it allows us to efficiently manage the current state of scores and provides quick access to the most recent scores for calculations.

---

![image.png](https://assets.leetcode.com/users/images/19485249-62d6-4ef3-9fb6-b6e5c406fc82_1735709643.7219408.png)


# Approach
1. **Stack for Score Tracking**: Use a stack to store the scores at each step. This enables efficient operations like addition, removal, and duplication of scores.
2. **Iterate through Operations**:
   - **Integer Push**: Parse the integer (handle both positive and negative values) and push it onto the stack.
   - **"C" Operation**: Remove the last score by popping the stack.
   - **"D" Operation**: Push double the value of the last score onto the stack.
   - **"+" Operation**: Sum the top two scores, push the result back onto the stack while maintaining the stack's integrity.
3. **Sum the Scores**: Once all operations are processed, sum up all values in the stack to get the total score.

---

# Complexity
- **Time Complexity**:  
  Each operation (push, pop, or top) takes \(O(1)\). Processing \(n\) operations requires \(O(n)\), where \(n\) is the size of the `operations` array.

- **Space Complexity**:  
  The stack stores scores for each operation, which in the worst case is \(O(n)\).

---

# Code
Here is your provided solution with a minor cleanup for readability:

```cpp
class Solution {
public:
    int calPoints(vector<string>& operations) {
        stack<int> st;

        for (string s : operations) {
            char c = s[0];

            if (c == '-') { // Handle negative integers
                int num = stoi(s);
                st.push(num);
            } else if (isdigit(c)) { // Handle positive integers
                int num = stoi(s);
                st.push(num);
            } else if (c == 'C') { // Remove the last score
                st.pop();
            } else if (c == 'D') { // Push double the last score
                st.push(st.top() * 2);
            } else if (c == '+') { // Push the sum of the last two scores
                int top1 = st.top(); st.pop();
                int top2 = st.top();
                st.push(top1); // Restore the stack
                st.push(top1 + top2);
            }
        }

        int ans = 0;
        while (!st.empty()) {
            ans += st.top();
            st.pop();
        }
        return ans;
    }
};
```

---

# Key Notes
1. **`stoi` Usage**: Instead of manually converting the string to an integer, `stoi` simplifies the process and handles both positive and negative numbers.
2. **Edge Cases**: Ensure to test cases like:
   - Operations with only "C" or "D".
   - Large inputs to confirm no overflow occurs.
   - Mixed operations in various orders.




['Array', 'Stack', 'Simulation', 'C++']