Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- two pointers
- Binary Search
- greedy
- Tree
- 파이썬
- sorting
- dynamic programming
- string
- 자바
- hash table
- 구현
- simulation
- Stack
- Number Theory
- implement
- 코테
- Matrix
- Math
- Method
- Class
- bit manipulation
- java
- database
- 코딩테스트
- SQL
- Binary Tree
- geometry
- Counting
- Data Structure
- array
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1614. Maximum Nesting Depth of the Parentheses 본문
코딩테스트 풀이/JAVA
[LeetCode/Easy] 1614. Maximum Nesting Depth of the Parentheses
무지맘 2022. 12. 29. 12:011. Input
1) VPS인 문자열 s
- VPS(Valid Parentheses String): 올바른 괄호를 사용한 문자열
2. Output
1) s의 nesting depth를 반환
- 1의 nesting depth는 0이다.
- ()의 nesting depth는 1이다.
- (())()의 nesting depth는 2이다.
3. Constraint
1) 1 <= s.length <= 100
2) s는 ‘0’~‘9’, '+', '-', '*', '/', '(', ')'로만 이루어져 있다.
4. Example
Input: s = "(1+(2*3)+((8)/4))+1" -> Output: 3
설명:
- depth가 가장 큰건 8일때다.
5. Code
1) 첫 코드(2022/07/01)
int cd = 0;
int md = 0;
for(int i=0 ; i<s.length() ; i++){
char c = s.charAt(i);
if(c=='('){
cd++;
if(cd>md) md = cd;
} else if(c==')')
cd--;
}
return md;
2) 스택을 이용해 풀어본 코드(2022/12/29)
import java.util.*;
int answer = 0, current = 0;
Stack st = new Stack();
for(int i=0 ; i<s.length() ; i++){
if(s.charAt(i)=='('){
st.push(0); current++;
}else if(s.charAt(i)==')'){
if(current>answer) answer = current;
st.pop(); current--;
}
}
return answer;
- 1번 코드와 시간은 차이 없었으나 메모리 활용에서 훨씬 좋은 결과를 보여줬다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1619. Mean of Array After Removing Some Elements (0) | 2022.12.29 |
---|---|
[LeetCode/Easy] 1608. Special Array With X Elements Greater Than or Equal X (0) | 2022.12.29 |
[프로그래머스/Lv.1] 과일 장수 (0) | 2022.12.28 |
[프로그래머스/Lv.0] k의 개수 (0) | 2022.12.27 |
[프로그래머스/Lv.2] 주식가격 (0) | 2022.12.27 |