A research submersible starts every mission at the surface, depth 0, and its onboard computer logs each maneuver in order as one of three kinds of command:
asc — the submersible ascends one level; if it is already at the surface, this command has no effect and the depth stays 0;stay — the submersible holds its current depth;After the whole mission log has been replayed in order, determine the minimum number of asc commands the submersible would still have to issue, one after another, to get back exactly to the surface (depth 0).
n, the number of commands in the log.n space-separated tokens, each either asc, stay, or a chamber name of 1 to 10 lowercase English letters (guaranteed to differ from both keywords).A single integer: the number of further asc commands required, i.e. the submersible's final depth after replaying the whole log.
1 <= n <= 1000.Example 1
Input
5 alpha beta asc gamma stay
Expected
2
Explanation
Two dives (`alpha`, `beta`) take the submersible to depth 2; `asc` brings it back up to depth 1; `gamma` dives again to depth 2; `stay` leaves it at depth 2. It would still need 2 ascends to reach the surface, so the output is 2.
Example 2
Input
3 asc asc alpha
Expected
1
Explanation
The submersible starts at the surface, so both leading `asc` commands do nothing (depth stays 0). The final command dives into chamber `alpha`, leaving the submersible at depth 1, so exactly one more ascend is needed, giving output 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →