Submit solution


Points: 10 (partial)
Time limit: 3.0s
Memory limit: 32M

Author:
Problem type
Allowed languages
Python

Design a stack that supports push, pop, top, and calculate the square sum of elements in the stack.

Please implement the following functions:

  1. init() initialize an empty stack and return None.
  2. push(val) push an integer element val into the top of stack and return None
  3. pop() removes the element at the top of the stack and return None.
  4. top() return the top element of the stack.
  5. getSquareSum() calculate the square sum of all elements in the stack, you should return 0 if the stack is empty. This function should be O(1)

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 N function calls in each testcase.

Limits

Memory limits: 32MB.

Time limits: 3 second.

1 \leq T \leq 100

1 \leq N \leq 10000

Ouput Specification

For each function call, you must return exactly what we asked for.

Test Set 1

Sample Case

Test Set 2

1 \leq N \leq 100

Test Set 3

1 \leq N \leq 1000

Test Set 4

1 \leq N \leq 10000

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


  • 0
    JMEOW  commented on Nov. 6, 2023, 8:17 p.m. edited

    :)