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
- hash table
- sorting
- SQL
- Math
- Number Theory
- Counting
- greedy
- Matrix
- array
- Method
- 자바
- java
- Binary Search
- bit manipulation
- 코테
- dynamic programming
- 구현
- Tree
- 코딩테스트
- Binary Tree
- geometry
- implement
- Data Structure
- 파이썬
- simulation
- string
- Stack
- database
- Class
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2103. Rings and Rods 본문
1. Input
1) 문자열 rings
- 링의 색깔과 링의 위치가 순서대로 담긴 길이 2의 문자열이 연속적으로 n개 담겨 있다.
- 첫 번째 문자는 색깔을, 두 번째 문자는 위치를 나타낸다.
2. Output
1) 0부터 9까지 레이블링 되어있는 막대기들 중 세 가지 링이 모두 있는 막대기의 번호를 반환
3. Constraint
1) rings.length == 2 * n
2) 1 <= n <= 100
3) rings는 'R', 'G', 'B', 0-9로 이루어져 있다.
4. Example
Input: num1=2, num2=3 -> Output: 6
5. Code
1) 첫 코드(2022/06/14)
int[] r = new int[10];
int[] g = new int[10];
int[] b = new int[10];
int n = 0;
for(int i=0 ; i<rings.length()/2 ; i++){
n = rings.charAt(2*i+1)-'0';
if(rings.charAt(2*i) == 'R' && r[n]==0) r[n]++;
else if(rings.charAt(2*i) == 'G' && G[n]==0) g[n]++;
else if(rings.charAt(2*i) == 'B' && B[n]==0) b[n]++;
}
for(int i=0 ; i<10 ; i++)
r[i] += (g[i] + b[i]);
int count = 0;
for(int i=0 ; i<10 ; i++)
if(r[i] == 3)
count++;
return count;
- 빠르지만 메모리를 많이 쓴다.
- 각 위치에 색깔별로 처음 1개만 들어왔을 때를 세려고 위와 같이 코딩을 했는데, 지금으로써는 딱히 더 좋은 코드가 생각이 나지 않는다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2108. Find First Palindromic String in the Array (0) | 2023.01.13 |
---|---|
[LeetCode/Easy] 66. Plus One (0) | 2023.01.12 |
[LeetCode/Easy] 2089. Find Target Indices After Sorting Array (0) | 2023.01.12 |
[LeetCode/Easy] 2068. Check Whether Two Strings are Almost Equivalent (0) | 2023.01.12 |
[LeetCode/Easy] 2057. Smallest Index With Equal Value (0) | 2023.01.12 |