반응형
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
int solution(int n, vector<int> lost, vector<int> reserve) {
int answer = 0;
bool visited[31] = { false, };
int cnt = 0; //여벌의 체육복으로 해결된 학생수
int len = lost.size(); //도난당한학생수
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])
{
cnt++;
lost[i] = 0; //0으로 초기화
reserve[j] = 0;
visited[j] = true;
}
}
}
for (int i = 0; i < lost.size(); i++) //앞번호, 뒷번호학생에게 빌릴수 있는지 확인
{
for (int j = 0; j < reserve.size(); j++)
{
if (lost[i] == 0 || reserve[j] == 0) continue;
if (lost[i] + 1 == reserve[j])
{
if (visited[j])continue;
visited[j] = true; //빌려준학생을 visited 배열에 표시
cnt++;
}
else if (lost[i] - 1 == reserve[j])
{
if (visited[j])continue;
visited[j] = true;
cnt++;
}
if (cnt >= len) //만약 도난당한 학생수보다 해결된학생이 커질경우
{
cnt = len; //도난당한학생수로 초기화후 종료
break;
}
}
}
answer = n - (len - cnt); //전체학생수 - (도난당한학생수 - 해결된학생수)
return answer;
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
return 0;
}
후기
level 1이였지만 생각보다 시간이 많이 소요됬다.
여벌체육복을 가져왔지만 도난된경우를 우선적으로 해결하지 않으면 정답이 될 수 없었다. (미리 해결하지않으면 여벌옷을 가져온 근처 번호인 학생으로 계산될 수 있기때문에)
미리 정렬을 해야할 필요성을 생각하지 못했다. (미리 정렬을해야 가까운 번호부터 우선적으로 확인가능하다)
반응형
'Programming > Programmers' 카테고리의 다른 글
프로그래머스 단속카메라 [c++] (0) | 2022.02.22 |
---|---|
프로그래머스 구명보트 [c++] (0) | 2022.02.21 |
프로그래머스 가장큰수 [c++] (0) | 2022.02.17 |
프로그래머스 이중우선순위큐 Level2 [c++] (0) | 2022.02.13 |
프로그래머스 완주하지 못한 선수 Level1 [c++] (0) | 2022.02.13 |