Skip to content

Commit db79e9c

Browse files
authored
Create maximum-nesting-depth-of-the-parentheses.cpp
1 parent f924f3e commit db79e9c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int maxDepth(string s) {
7+
int result = 0, curr = 0;
8+
for (const auto& c : s) {
9+
if (c == '(') {
10+
result = max(result, ++curr);
11+
} else if (c == ')') {
12+
--curr;
13+
}
14+
}
15+
return result;
16+
}
17+
};

0 commit comments

Comments
 (0)