1.
Approach
Our approach for this question is going to be pretty straightforward, we don't need to do anything special in this question we will just apply our basic knowledge of stack and complete the given functions.
Solution
We have implemented the solution in following ways:
Initialise two stacks s1 and min, s1 will store all values and min stack will have the minimum value.
MinStack will be empty as it will not do anything specific here.
In push function, push the value in main stack(s1), check if min stack is empty or value is less than or equal to current minimum value. If condition is true, it means value is new minimum, so it should be added to min stack.
For pop function ensure that s1 is not empty, check if top value of s1 is equal to top value of min. If so, means that we have to pop top value of min stack.
Pop top value from s1.
For top function, return top value of s1 stack.
For getMin function, return top value of min stack.
2.
Approaches
Iterate over all characters one by one.
If character is operand push it in the stack.
If character is operator pop two elements from start and perform operation.
Solution
We have achieved this solution by following ways:
Initialise a stack to store the result.
Iterate over the tokens vector.
Check if first character of token is digit, then check if it has atleast 2 characters and negative and second character must be digit.
If any of above is true convert string to int and push it in stack.
Else if the character is an operator, There are two operands at top of stack value 1 and value 2.
Since token is an operator now based on operator check the case and push the value.
The final result is at top of the stack.
I hope the solutions I have provided are understandable and explanations are easy to understand.
Thank You
You can connect with me on Linkedin
SOCIAL SHARE CARD GENERATOR