Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

ํ’€์ด

์นด๋“œ ๋ญ‰์น˜๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๊ณ  ํ•ญ์ƒ ์œ„์—์„œ๋งŒ ์ˆœ์„œ๋Œ€๋กœ ๋ฝ‘์•„์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์ด ์žˆ์–ด Queue ๋กœ ํ’€๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค. ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” ์„ธ ์ข…๋ฅ˜์˜ ์นด๋“œ ๋ญ‰์น˜๋ฅผ ๋ชจ๋‘ ๊ฐ๊ฐ Queue ์— ๋„ฃ๊ณ  ๊ฐ๊ฐ์˜ ์นด๋“œ๋ญ‰์น˜์˜ ๋งจ ์œ„ ์นด๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด goal ๊ณผ ๊ฐ™๋‹ค๋ฉด ํ•˜๋‚˜์”ฉ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค.

goalQ ๊ฐ€ ๋ชจ๋‘ ๋น„์—ˆ๋‹ค๋ฉด ์กฐ๊ฑด๋ฌธ์— ์˜ํ•ด ์ผ์น˜ํ•˜๋Š” ์นด๋“œ๊ฐ€ ๋ชจ๋‘ ์ œ๊ฑฐ๋œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— โ€œYesโ€ ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , card1Queue ์™€ card2Queue ์—์„œ ๊บผ๋‚ด์„œ ๋น„๊ตํ•˜๋Š” ๊ณผ์ •์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์นด๋“œ๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋Š” ์ฆ‰์‹œ โ€œNoโ€ ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {

  private static String check(Queue<String> card1Queue, Queue<String> card2Queue, Queue<String> goalQ) {
    while (!goalQ.isEmpty()) {
      if (!card1Queue.isEmpty() && card1Queue.peek().equals(goalQ.peek())) {
        card1Queue.poll();
        goalQ.poll();
      } else if (!card2Queue.isEmpty() && card2Queue.peek().equals(goalQ.peek())) {
        card2Queue.poll();
        goalQ.poll();
      } else {
        return "No";
      }
    }
    return "Yes";
  }

  public String solution(String[] cards1, String[] cards2, String[] goal) {

    Queue<String> card1Queue = new ArrayDeque<>(List.of(cards1));
    Queue<String> card2Queue = new ArrayDeque<>(List.of(cards2));
    Queue<String> goalQ = new ArrayDeque<>(List.of(goal));

    return check(card1Queue, card2Queue, goalQ);
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ