[프로그래머스] 체육복
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int N;
vector<int> stat;
int maxAnswer = 0;
void lend(int startIdx){
int noSuitIdx = -1;
for(int i = startIdx; i<N; ++i){
if(stat[i] == 0){
noSuitIdx = i;
break;
}
}
if(noSuitIdx == -1){
int answer = 0;
for(int i = 0; i<N; ++i){
if(stat[i] >= 1) answer++;
}
maxAnswer = max(maxAnswer, answer);
return;
}
lend(noSuitIdx+1);
if((noSuitIdx > 0) && (stat[noSuitIdx - 1] == 2)){
stat[noSuitIdx - 1] = 1;
stat[noSuitIdx] = 1;
lend(noSuitIdx+1);
stat[noSuitIdx - 1] = 2;
stat[noSuitIdx] = 0;
}
if((noSuitIdx < N-1) && (stat[noSuitIdx + 1] == 2)){
stat[noSuitIdx + 1] = 1;
stat[noSuitIdx] = 1;
lend(noSuitIdx+1);
stat[noSuitIdx + 1] = 2;
stat[noSuitIdx] = 1;
}
return;
}
int solution(int n, vector<int> lost, vector<int> reserve) {
N = n;
for(int i = 0; i<n; ++i){
stat.push_back(1);
}
for(int i = 0; i<lost.size(); ++i){
stat[lost[i]-1]--;
}
for(int i = 0; i<reserve.size(); ++i){
stat[reserve[i]-1]++;
}
for(int i = 0; i<N; ++i){
if(stat[i] >= 1) maxAnswer++;
}
lend(0);
int answer = maxAnswer;
return answer;
}