Skip to content

Commit fb04545

Browse files
committed
Solve: 문제 17 - 카드 뭉치
1 parent 65d12a1 commit fb04545

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Diff for: mezzang/book/17.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//문제 17 - 카드 뭉치
2+
class Queue{
3+
items = [];
4+
front = 0;
5+
rear = 0;
6+
7+
constructor(array) { //array를 매개변수로 받아 초기화를 해준다.
8+
this.items = array;
9+
this.rear = array.length;
10+
11+
}
12+
13+
push(item){
14+
this.items.push(item);
15+
this.rear++;
16+
}
17+
pop(){
18+
return this.items[this.front++];
19+
}
20+
21+
first() {
22+
return this.items[this.front];
23+
}
24+
isEmpty(){
25+
return this.front === this.rear;
26+
}
27+
}
28+
function solution(cards1, cards2, goal){
29+
cards1 = new Queue(cards1);
30+
cards2 = new Queue(cards2);
31+
goal = new Queue(goal);
32+
33+
while(!goal.isEmpty()){
34+
if(!cards1.isEmpty() && cards1.first() === goal.first()){
35+
cards1.pop();
36+
goal.pop();
37+
} else if(!cards2.isEmpty() && cards2.first() === goal.first()){
38+
cards2.pop();
39+
goal.pop();
40+
} else {
41+
break;
42+
}
43+
}
44+
return goal.isEmpty() ? "Yes":"No";
45+
}
46+
console.log(solution(["i","drink","water"],["want","to"],["i","want","to","drink","water"]));

0 commit comments

Comments
 (0)