기몽수
코딩 기록일지
기몽수
전체 방문자
오늘
어제
  • 분류 전체보기 (443)
    • 알고리즘 - SWEA (210)
      • D1 (19)
      • D2 (25)
      • D3 (143)
      • D4 (21)
      • D5 (2)
    • 알고리즘 - Programmers (74)
      • Unrated (34)
      • Lv 0 (4)
      • Lv 1 (3)
      • Lv 2 (32)
      • Lv 3 (1)
    • 알고리즘 - Baekjoon (158)
      • Bronze (1)
      • Silver (65)
      • Gold (90)
      • Platinum (2)
    • 취업 (0)
    • SSAFY (1)
hELLO · Designed By 김용수.
기몽수

코딩 기록일지

알고리즘 - SWEA/D3

[SW expert Academy] SWEA 10761번 신뢰 자바(Java)

2023. 11. 12. 20:49

[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
    '알고리즘 - SWEA/D3' 카테고리의 다른 글
    • [SW expert Academy] SWEA 6900번 주혁이의 복권 당첨 자바(Java)
    • [SW expert Academy] SWEA 3376번 파도반 수열 자바(Java)
    • [SW expert Academy] SWEA 10032번 과자 분배 자바(Java)
    • [SW expert Academy] SWEA 7732번 시간 개념 자바(Java)
    기몽수
    기몽수

    티스토리툴바