풀이
1. 앞뒤로 모두 체육복을 빌릴 수 있다면 앞에 있는 체육복을 빌린다.
(뒤에 있는 체육복은 2번 앞에 있는 사람이 빌릴수도 있기 때문에)
2. 여벌을 가지고 왔지만 도난당한 사람은 본인 것을 입어야하기 때문에
체육복을 입을 수는 있지만 앞뒤로 빌려줄 수 없다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool students[32];
int solution(int n, vector<int> lost, vector<int> reserve) {
int answer = 0;
for(int i=0;i<reserve.size();i++){
students[reserve[i]]=true;
}
sort(lost.begin(),lost.end());
int cnt=0;
// (1) 여벌을 가지고 왔지만 도난당한 사람
for(int i=0;i<lost.size();i++){
if(students[lost[i]]){
students[lost[i]]=false;
cnt++;
lost[i]=-1;
}
}
for(int i=0;i<lost.size();i++){
//(1)은 빌릴 수 없음
if(lost[i]==-1){
continue;
}
if(students[lost[i]-1] )
{ students[lost[i]-1]=false;
cnt++;
}
else if(students[lost[i]+1]){
students[lost[i]+1]=false;
cnt++;
}
}
return answer= n - (lost.size()-cnt);
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 큰 수 만들기 c++ (0) | 2024.01.29 |
---|---|
[프로그래머스] 조이스틱 c++ (1) | 2024.01.26 |
[프로그래머스] 모음사전 c++ (0) | 2024.01.22 |
[프로그래머스] 피로도 c++ (0) | 2024.01.22 |
[프로그래머스] 카펫 c++ (0) | 2024.01.22 |