Skip to content

Commit 28d0c95

Browse files
[level 1] Title: 카드 뭉치, Time: 24.44 ms, Memory: 65.2 MB -BaekjoonHub
1 parent 7b48f31 commit 28d0c95

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# [level 1] 카드 뭉치 - 159994
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/159994)
4+
5+
### 성능 요약
6+
7+
메모리: 65.2 MB, 시간: 24.44 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 연습문제
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2024년 11월 27일 20:13:55
20+
21+
### 문제 설명
22+
23+
<p>코니는 영어 단어가 적힌 카드 뭉치 두 개를 선물로 받았습니다. 코니는 다음과 같은 규칙으로 카드에 적힌 단어들을 사용해 원하는 순서의 단어 배열을 만들 수 있는지 알고 싶습니다.</p>
24+
25+
<ul>
26+
<li>원하는 카드 뭉치에서 카드를 순서대로 한 장씩 사용합니다.</li>
27+
<li>한 번 사용한 카드는 다시 사용할 수 없습니다.</li>
28+
<li>카드를 사용하지 않고 다음 카드로 넘어갈 수 없습니다.</li>
29+
<li>기존에 주어진 카드 뭉치의 단어 순서는 바꿀 수 없습니다.</li>
30+
</ul>
31+
32+
<p>예를 들어 첫 번째 카드 뭉치에 순서대로 ["i", "drink", "water"], 두 번째 카드 뭉치에 순서대로 ["want", "to"]가 적혀있을 때 ["i", "want", "to", "drink", "water"] 순서의 단어 배열을 만들려고 한다면 첫 번째 카드 뭉치에서 "i"를 사용한 후 두 번째 카드 뭉치에서 "want"와 "to"를 사용하고 첫 번째 카드뭉치에 "drink"와 "water"를 차례대로 사용하면 원하는 순서의 단어 배열을 만들 수 있습니다.</p>
33+
34+
<p>문자열로 이루어진 배열 <code>cards1</code>, <code>cards2</code>와 원하는 단어 배열&nbsp;<code>goal</code>이 매개변수로 주어질 때, <code>cards1</code>과 <code>cards2</code>에 적힌 단어들로 <code>goal</code>를 만들 있다면 "Yes"를, 만들 수 없다면 "No"를 return하는 solution 함수를 완성해주세요.</p>
35+
36+
<hr>
37+
38+
<h5>제한사항</h5>
39+
40+
<ul>
41+
<li>1 ≤ <code>cards1</code>의 길이, <code>cards2</code>의 길이 ≤ 10
42+
43+
<ul>
44+
<li>1 ≤ <code>cards1[i]</code>의 길이, <code>cards2[i]</code>의 길이 ≤ 10</li>
45+
<li><code>cards1</code>과 <code>cards2</code>에는 서로 다른 단어만 존재합니다.</li>
46+
</ul></li>
47+
<li>2 ≤ <code>goal</code>의 길이 ≤ <code>cards1</code>의 길이 + <code>cards2</code>의 길이
48+
49+
<ul>
50+
<li>1 ≤ <code>goal[i]</code>의 길이 ≤ 10</li>
51+
<li><code>goal</code>의 원소는 <code>cards1</code>과 <code>cards2</code>의 원소들로만 이루어져 있습니다.</li>
52+
</ul></li>
53+
<li><code>cards1</code>, <code>cards2</code>, <code>goal</code>의 문자열들은 모두 알파벳 소문자로만 이루어져 있습니다.</li>
54+
</ul>
55+
56+
<hr>
57+
58+
<h5>입출력 예</h5>
59+
<table class="table">
60+
<thead><tr>
61+
<th>cards1</th>
62+
<th>cards2</th>
63+
<th>goal</th>
64+
<th>result</th>
65+
</tr>
66+
</thead>
67+
<tbody><tr>
68+
<td>["i", "drink", "water"]</td>
69+
<td>["want", "to"]</td>
70+
<td>["i", "want", "to", "drink", "water"]</td>
71+
<td>"Yes"</td>
72+
</tr>
73+
<tr>
74+
<td>["i", "water", "drink"]</td>
75+
<td>["want", "to"]</td>
76+
<td>["i", "want", "to", "drink", "water"]</td>
77+
<td>"No"</td>
78+
</tr>
79+
</tbody>
80+
</table>
81+
<hr>
82+
83+
<h5>입출력 예 설명</h5>
84+
85+
<p>입출력 예 #1</p>
86+
87+
<p>본문과 같습니다.</p>
88+
89+
<p>입출력 예 #2</p>
90+
91+
<p><code>cards1</code>에서 "i"를 사용하고 <code>cards2</code>에서 "want"와 "to"를 사용하여 "i want to"까지는 만들 수 있지만 "water"가 "drink"보다 먼저 사용되어야 하기 때문에 해당 문장을 완성시킬 수 없습니다. 따라서 "No"를 반환합니다.</p>
92+
93+
94+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
fun solution(cards1: Array<String>, cards2: Array<String>, goal: Array<String>): String {
3+
val queue1 = ArrayDeque(cards1.asList())
4+
val queue2 = ArrayDeque(cards2.asList())
5+
val goalQueue = ArrayDeque(goal.asList())
6+
7+
while (goalQueue.isNotEmpty()) {
8+
val currentGoal = goalQueue.removeFirst()
9+
10+
when {
11+
queue1.isNotEmpty() && currentGoal == queue1.first() -> queue1.removeFirst()
12+
queue2.isNotEmpty() && currentGoal == queue2.first() -> queue2.removeFirst()
13+
else -> return "No"
14+
}
15+
}
16+
return "Yes"
17+
}
18+
}

0 commit comments

Comments
 (0)