항해99를 하면서 풀어봤던 문제지만 다시 한 번 풀어보기
https://www.acmicpc.net/problem/25192
- 예제 입력1
9
ENTER
pjshwa
chansol
chogahui05
lms0806
pichulia
r4pidstart
swoon
tony9402
- 예제 출력1
8
- 예제 입력2
7
ENTER
pjshwa
chansol
chogahui05
ENTER
pjshwa
chansol
- 예제 출력2
5
- 예제 입력3
3
ENTER
lms0806
lms0806
- 예제 출력3
1
- 문제 접근
- ENTER : 새로운 사람이 채팅방에 입장
- 그 외 : 유저 닉네임
- ENTER 이후 첫 닉네임 = 인사
- 인사 이후 동일 닉네임 = 일반 채팅
- 입력
- 첫째 줄 : 채팅방 로그 N (1 <= N <= 100,000)
- ~ N번째 줄 : ENTER or 닉네임 (1 <= 문자열 길이 <= 20)
- 첫 로그는 반드시 ENTER
- 출력
- 곰곰티콘(인사) 사용 횟수 출력
- 문제 해결
- N번 반복
- 로그가 ENTER일 경우 Set의 Size를 count 변수에 더해넣고 초기화
- 한 로그에 여러 번의 ENTER가 들어올 수 있음
- 로그가 닉네임일 경우 Set에 add
- 반복문 종료 후 count 출력
Set.clear() 초기화 [메모리 : 25,532 KB / 시간 : 256 ms]
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Set<String> set = new HashSet<>();
int logCount = Integer.parseInt(br.readLine());
int GomGomCount = 0;
for(int i = 0; i < logCount; i++){
String input = br.readLine();
if(!input.equals("ENTER")) {
set.add(input);
continue;
}
GomGomCount += set.size();
set.clear();
}
GomGomCount += set.size();
System.out.println(GomGomCount);
}
새 객체 할당 [메모리 : 28,792 KB / 시간 : 248 ms]
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Set<String> set = new HashSet<>();
int logCount = Integer.parseInt(br.readLine());
int GomGomCount = 0;
for(int i = 0; i < logCount; i++){
String input = br.readLine();
if(!input.equals("ENTER")) {
set.add(input);
continue;
}
GomGomCount += set.size();
set = new HashSet<>();
}
GomGomCount += set.size();
System.out.println(GomGomCount);
}
set.clear()와 new HashSet<>()을 이용한 새 객체 할당하는 방식 차이
- set.clear()
- Set 안에 있는 모든 요소를 제거
- GC(가비지 컬렉터)의 비용이 발생하지 않음
- Set 안의 요소를 제거하기 때문에 엄청나게 많은 양의 데이터가 저장되어 있다면, 모든 요소를 제거하는 데 시간이 소요 됨
public static void main(String[] args) {
final int LENGTH = 100_000_000;
Set<Integer> set = new HashSet<>();
for(int i = 1; i <= LENGTH; i++){
set.add(i);
}
long begin = System.currentTimeMillis();
set.clear();
System.out.println(LENGTH + "개의 데이터 Clear = " + (System.currentTimeMillis() - begin) + " ms");
// 결과 : 100000000개의 데이터 Clear = 112 ms
}
- new HashSet<>()을 이용한 새 객체 할당
- 기존 객체를 버리고 새로운 객체를 생성
- 기존 Set의 사이즈가 매우 크더라도 별다른 문제가 발생하지 않음
- 새로운 객체를 할당하기 때문에 메모리 사용량이 증가될 수 있음
- 기존 객체를 버렸기 때문에 GC의 비용이 발생
public static void main(String[] args) {
final int LENGTH = 100_000_000;
Set<Integer> set = new HashSet<>();
for(int i = 1; i <= LENGTH; i++){
set.add(i);
}
long begin = System.currentTimeMillis();
set = new HashSet<>();
System.out.println("새 객체 할당 = " + (System.currentTimeMillis() - begin) + " ms");
// 결과 : 새 객체 할당 = 0 ms
}
'Coding Test > Step20. 심화 2' 카테고리의 다른 글
[Java] 5단계. 영단어 암기는 괴로워 [20920번] (0) | 2024.11.19 |
---|---|
[Java] 4단계. 통계학 [2108번] (0) | 2024.11.15 |
[Java] 3단계. 붙임성 좋은 총총이 [26069번] (1) | 2024.11.15 |
[Java] 1단계. 약수 [1037번] (0) | 2024.11.15 |