[BaekJoon] 1966 프린터 큐 (java)

🔗 문제 링크

https://www.acmicpc.net/problem/1966


👨🏻‍💻 내가 작성한 코드

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
		
		int testCaseNum = sc.nextInt();
				
		ArrayList<Integer> queue = new ArrayList<>();
		for (int i=0; i<testCaseNum; i++) {
			int count = 0;
			queue.clear();
			int inputNum = sc.nextInt();
			int position = sc.nextInt();
			
			for(int j=0; j< inputNum; j++) {
				queue.add(sc.nextInt());
			}
			
			int len = queue.size();
				
			while(queue.size() != 0) {
				int max = Collections.max(queue);
				int removeNum = queue.remove(0);			

				if (removeNum != max) {
					queue.add(removeNum);
					if (position == 0) position = queue.size()-1;
					else position--;
				}
				else {
					count++;
					if (position == 0) {
						System.out.println(count);
						break;
					}
					position--;
				}
				
			}
		}		
	}
}

좋은 웹페이지 즐겨찾기