LeetCode Entry
1614. Maximum Nesting Depth of the Parentheses
Max nested braces depth
1614. Maximum Nesting Depth of the Parentheses easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1496
Problem TLDR
Max nested braces depth
Intuition
Scan and calculate the max of a running sum.
Approach
- x.compareTo(y) gives -1,0,1
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun maxDepth(s: String) =
s.scan(0) {r,c->r+(c=='(').compareTo(c==')')}.max()
pub fn max_depth(s: String) -> i32 {
s.bytes().fold((0,0),|(d,m),b|{let d=d+(b==40)as i32-(b==41)as i32;(d,m.max(d))}).1
}
Comments