🔵 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/159994
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
🔵 알고리즘
- 카드 뭉치를 큐로 변환한다.
- 각 카드 뭉치의 front와 필요한 단어를 비교해서 yes or no 리턴
🔵 전체 코드
#include <string>
#include <queue>
#include <vector>
using namespace std;
// 각 카드 뭉치의 front와 필요한 단어를 비교해서 yes or no 리턴
string solution(vector<string> cards1, vector<string> cards2, vector<string> goal) {
string answer = "Yes";
queue<string> c1, c2;
for (string str : cards1) c1.push(str);
for (string str : cards2) c2.push(str);
for (int i = 0; i < goal.size(); i++)
{
string desire = goal[i];
if (!c1.empty() && c1.front() == desire)
{
c1.pop();
}
else if (!c2.empty() && c2.front() == desire)
{
c2.pop();
}
else
{
answer = "No";
break;
}
}
return answer;
}
🔵 시간 복잡도
카드 뭉치 1의 길이를 N, 카드 뭉치 2의 길이를 M이라 했을 때, goal의 길이는 최대 N + M 이므로 시간 복잡도는 O(N + M)이다.
'PS' 카테고리의 다른 글
| [C++] Programmers Lv. 2 "영어 끝말잇기" (0) | 2025.05.22 |
|---|---|
| [C++] Programmers Lv. 1 "완주하지 못한 선수" (0) | 2025.05.22 |
| [C++] Programmers Lv. 2 "기능 개발" (0) | 2025.05.16 |
| [C++] Programmers Lv. 3 "표 편집" (0) | 2025.05.15 |
| [C++] Programmers Lv. 1 "크레인 인형 뽑기 게임" (0) | 2025.05.09 |