A pretty large test case is added, making my previously ACed JAVA solution TLE.
I guess it's due to the frequent stack operations (reallocation memory).
I worked around this by using ArrayList to simulate the Stack and specify the init capacity to avoid memory reallocation. It works.
Not sure if there is better solution.