NeetCode
Evaluate Reverse Polish Notation

14. Evaluate Reverse Polish Notation

Medium

Design a dynamic array class. The data structure should expose indexed reads/writes, support appending/removing from the end, and report its current capacity.

dynamic array Resizable Array ArrayList vector

Your DynamicArray class should support the following operations:

  • DynamicArray(int capacity) : Initialize an empty array with the provided capacity.
  • int get(int i) : Return the element at index i. Assume 0 <= i < length.
  • void set(int i, int n) : Set the element at index i to n.
  • void pushback(int n) : Add element n to the end of the array.
  • int popback() : Remove and return the element at the end of the array.
  • int getCapacity() : Return the current capacity of the array.

If pushback() is called when the array is full, double the capacity before inserting the new element.

Example 1:

Input:
["push", 1, "getCap", "push", 2]

Output:
[null, null, 1, null]

Constraints:

  • 1 <= capacity <= 1000
  • 0 <= n <= 1000

Input

capacity = 1
ops = [push(1), get(0), pushback(2)]