STUDYING/Algorithm
[Programmers] 체육복
EOZIN
2021. 9. 27. 00:36
728x90
https://programmers.co.kr/learn/courses/30/lessons/42862
#include <string>
#include <vector>
using namespace std;
int solution(int n, vector<int> lost, vector<int> reserve) {
int answer = 0;
vector<int> students(n, 1);
for (int i = 0; i < lost.size(); ++i)
students[lost[i] - 1]--;
for (int i = 0; i < reserve.size(); ++i)
students[reserve[i] - 1]++;
for (int i = 0; i < n; i++) {
if (i != 0 && students[i] == 0) {
if (students[i - 1] == 2) {
students[i]++;
students[i - 1]--;
continue;
}
}
if (i != n - 1 && students[i] == 0) {
if (students[i + 1] == 2) {
students[i]++;
students[i + 1]--;
}
}
}
for (int i = 0; i < n; ++i) {
if (students[i] > 0) {
answer++;
}
}
return answer;
}