백준 자바 18528번 큐2 - 시간 초과 StringBuilder로 해결!

이번 문제는 백준 18528번 큐2로 이전 시간에 공부했던 BufferedReader는 입력 시간을 줄여주기 위한 것이고

StringBuilder는 출력시간을 줄여주기 위해 배워보겠습니다.

 

이전에 BufferedReader를 통해 시간초과를 해결해봤었습니다!

 

실패한 코드는 아래와 같습니다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer; 
public class Main {
	
	static LinkedList<Integer> q = new LinkedList<>();
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		StringTokenizer command;
		
		for (int i=0; i<N; i++) {
			command = new StringTokenizer(br.readLine(), " ");
			String cmd = command.nextToken();
			
			if(cmd.equals("push")) {
				int X = Integer.parseInt(command.nextToken());
				push(X);
			}
			
			else if(cmd.equals("size")) {
				System.out.println(size());
			}
			
			else if(cmd.equals("pop")) {
				System.out.println(pop());
			}
			
			else if(cmd.equals("empty")) {
				System.out.println(empty());
			}
			
			else if(cmd.equals("front")) {
				System.out.println(front());
			}
			
			else if(cmd.equals("back")) {
				System.out.println(back());
			}
		}
	}
		
	
	
	public static void push(int X) {
		q.addLast(X);
	}
	
	public static int pop() {
		if (!q.isEmpty()) {
			return q.removeFirst();
		}
		else {
			return -1;
		}
	}
	
	public static int size() {
		return q.size();
	}
	
	public static int empty() {
		if (q.isEmpty()) {
			return 1;
		}
		else {
			return 0;
		}
		
	}
		
	public static int front() {	
		if (!q.isEmpty()) {
			return q.peek();
		}
		else {
			return -1;
		}
	}	
	
	public static int back() {	
		if (!q.isEmpty()) {
			return q.getLast();
		}
		else {
			return -1;
		}
	}
}

 

어? 왜 시간초과지? 문제는 println의 성능 때문이었습니다.

 

시간 제한때문에 버렸던 Scanner 

이제는 System.out.println을 버릴 차례입니다.

 

StringBuilder 가 무엇이냐!

BufferedReader가 버퍼에 모아놓는 방식처럼 모아놨다가 한번에 출력하는 방식입니다. 

 

StringBuilder sb = new StringBuilder();

 

생성자는 이렇습니다. 

 

그리고 이런식으로 append를 통해 sb에 넣어줍니다. 개행문자 \n을 넣어주는 이유는 마지막에 출력할때

101-1 이런식으로 출력하려는게 아니기 때문입니다.

sb.append(size()).append("\n");

 

 

모아놨던 sb를 문자열로 변환시켜주고 출력하면 됩니다.

System.out.println(sb.toString());

 

package baekjoon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;



 
public class Queue2 {
	
	static LinkedList<Integer> q = new LinkedList<>();
	
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		StringTokenizer command;
		StringBuilder sb = new StringBuilder();
		
		for (int i=0; i<N; i++) {
			command = new StringTokenizer(br.readLine(), " ");
			String cmd = command.nextToken();
			
			if(cmd.equals("push")) {
				int X = Integer.parseInt(command.nextToken());
				push(X);
			}
			
			else if(cmd.equals("size")) {
				sb.append(size()).append("\n");
				
			}
			
			else if(cmd.equals("pop")) {
				sb.append(pop()).append("\n");
				
			}
			
			else if(cmd.equals("empty")) {
				sb.append(empty()).append("\n");
				
			}
			
			else if(cmd.equals("front")) {
				sb.append(front()).append("\n");
				
			}
			
			else if(cmd.equals("back")) {
				sb.append(back()).append("\n");
				
			
		
			}
		}
		
		System.out.println(sb.toString());
	}
		
	
	
	public static void push(int X) {
		q.addLast(X);
	}
	
	public static int pop() {
		if (!q.isEmpty()) {
			return q.removeFirst();
		}
		
		else {
			return -1;
		}
	}
	
	public static int size() {
		return q.size();
	}
	
	public static int empty() {
		if (q.isEmpty()) {
			return 1;
		}
		
		else {
			return 0;
		}
		
	}
		
	public static int front() {	
		if (!q.isEmpty()) {
			return q.peek();
		}
		
		else {
			return -1;
		}
	}	
	
	public static int back() {	
		if (!q.isEmpty()) {
			return q.getLast();
		}
		
		else {
			return -1;
		}
	}	
	
}

 

 

좋습니다.