Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Resolves: Leetcode 716
Main Techniques:
Runtime Complexity Analysis
Space Complexity Analysis
Testing
Performance
Performance Metrics from OJ Platform
Performance Improved Since Last Change
Implementation Notice
The tricky part of the implementation is
popMax()
, which can be seen by the following test case:The correct result is
The commonly incorrect result we can get is
The root cause is once we remove the max element, we also need to update the new max element of the stack, which is tricky to do. To get implementation correctly, notice where we use
pop()
andpush()
from our own implementation rather than using thestack
library implementation.Implementation Tricks
To Do
We need to implement the test infrastructure similar to leetcode's test case, like the one shown above.
To Learn
There exists a more efficient solution. Probably this.
Questions
Language
Failure Attempts
The above implementation comes from EPI 8.1, which is correct if we only need
peakMax()
and nopopMax()
involved.