[D3] 신뢰 - 10761
성능 요약
메모리: 40,912 KB, 시간: 218 ms, 코드길이: 2,371 Bytes
제출 일자
2023-11-12 20:47
출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
class Solution {
static class Button {
char color;
int idx;
public Button(char color, int idx) {
this.color = color;
this.idx = idx;
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
int n = sc.nextInt();
Queue<Button> buttons = new LinkedList<>();
Queue<Button> blues = new LinkedList<>();
Queue<Button> oranges = new LinkedList<>();
for (int i = 0; i < n; i++) {
char color = sc.next().charAt(0);
int idx = sc.nextInt();
Button button = new Button(color, idx);
if (color == 'B') {
blues.offer(button);
} else {
oranges.offer(button);
}
buttons.offer(button);
}
int blue = 0;
int orange = 0;
int time = 0;
boolean click = false;
while (!buttons.isEmpty()) {
time++;
click = false;
if (!blues.isEmpty()) {
if (blue < blues.peek().idx) {
blue++;
} else if (blue > blues.peek().idx) {
blue--;
} else {
if (buttons.peek().color == 'B') {
click = true;
blues.poll();
}
}
}
if (!oranges.isEmpty()) {
if (orange < oranges.peek().idx) {
orange++;
} else if (orange > oranges.peek().idx) {
orange--;
} else {
if (buttons.peek().color == 'O') {
click = true;
oranges.poll();
}
}
}
if (click) {
buttons.poll();
}
}
System.out.println("#" + tc + " " + (time - 1));
}
}
}
'알고리즘 - SWEA > D3' 카테고리의 다른 글
[SW expert Academy] SWEA 6900번 주혁이의 복권 당첨 자바(Java) (1) | 2023.11.13 |
---|---|
[SW expert Academy] SWEA 3376번 파도반 수열 자바(Java) (1) | 2023.11.13 |
[SW expert Academy] SWEA 10032번 과자 분배 자바(Java) (0) | 2023.11.12 |
[SW expert Academy] SWEA 7732번 시간 개념 자바(Java) (0) | 2023.11.12 |
[SW expert Academy] SWEA 9480번 민정이와 광직이의 알파벳 공부 자바(Java) (0) | 2023.11.12 |