사용언어 : C++
여러가지 예외상황만 잘 생각해주면 간단한 문제
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solution(int n, vector<int> lost, vector<int> reserve) {
vector<int> list(n, 1);
int answer=0;
for (int i = 0; i < lost.size(); i++) {
list[lost[i] - 1]--;
}
for (int i = 0; i < reserve.size(); i++) {
list[reserve[i] - 1]++;
}
for (int i = 0; i < list.size(); i++) {
if (list[i] == 0) {
if (i != 0 && list[i - 1] == 2) {
list[i - 1]--;
list[i]++;
}
else if (i != list.size() - 1 && list[i + 1] == 2) {
list[i + 1]--;
list[i]++;
}
}
}
for (int i = 0; i < list.size(); i++) {
if (list[i] !=0) {
answer++;
}
}
return answer;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solution(int n, vector<int> lost, vector<int> reserve) {
int answer = 0;
int clothes[32];
fill_n(clothes,32,1);
for(int i=0; i<lost.size(); i++){
clothes[lost[i]] = 0;
}
for(int i=0; i<reserve.size(); i++){
clothes[reserve[i]] = 2;
}
for(int l : lost){
for(int r : reserve){
if (l == r){
clothes[l] = 1;
}
}
}
for(int i=1; i<n+1; i++){
if (clothes[i] > 1 && clothes[i+1] == 0){
clothes[i]--;
clothes[i+1]++;
}else if (clothes[i] > 1 && clothes[i-1] == 0){
clothes[i]--;
clothes[i-1]++;
}
}
for(int i=1; i<n+1; i++){
if(clothes[i] > 0){
answer++;
}
}
return answer;
}