Design a stack that supports push
, pop
, top
, and calculate the square sum of elements in the stack.
Please implement the following functions:
init()
initialize an empty stack and return None.push(val)
push an integer element val into the top of stack and return Nonepop()
removes the element at the top of the stack and return None.top()
return the top element of the stack.getSquareSum()
calculate the square sum of all elements in the stack, you should return0
if the stack is empty. This function should be
Methods pop
, top
will always be called on non-empty stack.
You only need to submit the functions init()
, push(val)
, pop()
, top()
, and getSquareSum()
. You should not read from stdin
or write to stdout
, e.g. calling print()
and input()
.
Input Specification
The sample input shows how your functions will be called.
Our first function call will always be init()
.
init()
function may be invoked more than once in each test case.
You should initialize a new stack each time the init()
function is invoked.
We will make at most function calls in each testcase.
Limits
Memory limits: 32MB.
Time limits: 3 second.
Ouput Specification
For each function call, you must return exactly what we asked for.
Test Set 1
Sample Case
Test Set 2
Test Set 3
Test Set 4
Sample Input
init()
push(1)
push(2)
pop()
push(5)
getSquareSum()
top()
Sample Output
None
None
None
None
None
26
5
Explaination
Hint
If you encounter a TLE
error, you may check this link .
Comments
:)