[Hash] 13414번 - 수강신청

안수진·2023년 12월 19일

Baekjoon

목록 보기
11/55
post-thumbnail

🔗 문제 링크

[백준] 13414번 - 수강신청

📝 풀이

지켜야 할 규칙
1. 수강신청 클릭 2번 이상 -> 중복 삭제 후 대기열 맨 뒤로
2. 최대 수강 가능 인원 n명의 학번 출력

학생들의 학번을 저장할 때
중복을 허용하지 않으면서 순서를 보장하는 LinkedHashSet 사용

ArrayList 사용해서 전체 리스트를 돌면 시간초과가 뜬다

if(studentId.contains(id)) {
       studentId.remove(id);
}
studentId.add(id);

contains() 메소드를 사용하여 저장된 학번이 있으면
해당 학번 삭제 후 다시 추가


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class 수강신청_13414 {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());

        int max = Integer.parseInt(st.nextToken());
        int size = Integer.parseInt(st.nextToken());

        Set<String> studentId = new LinkedHashSet<>();

        for (int i = 0; i < size; i++) {
            String id = br.readLine();
            
            if(studentId.contains(id)) {
            	studentId.remove(id);
            }
            studentId.add(id);
        }

        int count = 0;
        for (String id : studentId) {
        	System.out.println(id);
            count++;
            if (count == max) break;
        }
        
	}
}
profile
항상 궁금해하기

0개의 댓글