LeetCode Entry
1614. Maximum Nesting Depth of the Parentheses
Max nested parenthesis
1614. Maximum Nesting Depth of the Parentheses easy
blog post
substack
youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/560
Problem TLDR
Max nested parenthesis #easy
Intuition
No special intuition, just increase or decrease a counter.
Approach
- There is a
maxOfin Kotlin, but solution is not pure functional. It can be withfold.
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun maxDepth(s: String): Int {
var curr = 0
return s.maxOf {
if (it == '(') curr++
if (it == ')') curr--
curr
}
}
pub fn max_depth(s: String) -> i32 {
let (mut curr, mut max) = (0, 0);
for b in s.bytes() {
if b == b'(' { curr += 1 }
if b == b')' { curr -= 1 }
max = max.max(curr)
}
max
}