-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1614MaximumNestingParenthesesDepth.java
More file actions
47 lines (40 loc) · 1.49 KB
/
Q1614MaximumNestingParenthesesDepth.java
File metadata and controls
47 lines (40 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
@b-knd (jingru) on 03 August 2022 11:26:00
*/
class Solution {
//solution using stack
public int maxDepth(String s) {
int max = 0;
Stack<String> stack = new Stack<>();
//if string is open parentheses, push to stack and update max, else pop from stack to remove opening parentheses
for(String str: s.split("")){
if(str.equals("(")){
stack.push("(");
max = Math.max(max, stack.size());
} else if(str.equals(")")){
stack.pop();
}
}
return max;
}
//Runtime: 7 ms, faster than 8.64% of Java online submissions for Maximum Nesting Depth of the Parentheses.
//Memory Usage: 42.8 MB, less than 6.68% of Java online submissions for Maximum Nesting Depth of the Parentheses.
//string-based solution
//similar to stack concept but using an integer variable instead of stack to keep track of depth
public int maxDepth(String s) {
int max = 0;
int j = 0;
for(Character c: s.toCharArray()){
if(c == '('){
j++;
}
if(c == ')'){
max = Math.max(j, max);
j -= 1;
}
}
return max;
}
//Runtime: 1 ms, faster than 87.77% of Java online submissions for Maximum Nesting Depth of the Parentheses.
//Memory Usage: 40 MB, less than 99.67% of Java online submissions for Maximum Nesting Depth of the Parentheses.
}