문제
풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(int n, vector<int> lost, vector<int> reserve)
{
int answer = n;
sort(lost.begin(), lost.end());
sort(reserve.begin(), reserve.end());
for (int i = 0; i < lost.size(); i++)
{
for (int j = 0; j < reserve.size(); j++)
{
if (lost[i] == reserve[j])
{
answer++;
lost[i] = -1;
reserve[j] = -1;
break;
}
}
}
for (auto lostNumber = lost.begin(); lostNumber != lost.end(); lostNumber++)
{
bool check = false;
for (auto reserveNumber = reserve.begin(); reserveNumber != reserve.end(); reserveNumber++)
{
if (lostNumber[0] == reserveNumber[0] + 1 || lostNumber[0] == reserveNumber[0] - 1)
{
check = true;
reserve.erase(reserveNumber);
break;
}
}
if (!check)
{
answer--;
}
}
return answer;
}
'코딩테스트' 카테고리의 다른 글
[코딩테스트] 카펫 (0) | 2021.08.22 |
---|---|
[코딩테스트] 타겟 넘버 (0) | 2021.08.20 |
[코딩테스트] 두 정수의 합 (0) | 2021.08.12 |
[코딩테스트] 다리를 지나는 트럭 (0) | 2021.08.10 |
[코딩테스트] 기능개발 (0) | 2021.08.08 |