#include <string>
#include <vector>
#include <iostream>
using namespace std;
int solution(int n, vector<int> lost, vector<int> reserve) {
vector<int> stu(n + 1, 1);
for (auto e : lost)
--stu[e];
for (auto e : reserve)
++stu[e];
for (int i = 1; i <= n; ++i) {
if (stu[i] == 0) {
if (i != 1 && stu[i - 1] == 2) { // 앞 번호 학생부터 빌려준다.
--stu[i - 1];
++stu[i];
} else if (i + 1 <= n && stu[i + 1] == 2) {
--stu[i + 1];
++stu[i];
}
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (stu[i] >= 1)
++ans;
}
return ans;
}
import java.util.*;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
ArrayList<Integer> stu = new ArrayList<>(Collections.nCopies(n + 1, 1));
for (var e : lost)
stu.set(e, stu.get(e) - 1);
for (var e : reserve)
stu.set(e, stu.get(e) + 1);
for (int i = 1; i <= n; ++i) {
if (stu.get(i) == 0) {
if (i != 1 && stu.get(i - 1) == 2) {
stu.set(i - 1, stu.get(i - 1) - 1);
stu.set(i, stu.get(i) + 1);
} else if (i + 1 <= n && stu.get(i + 1) == 2) {
stu.set(i + 1, stu.get(i + 1) - 1);
stu.set(i, stu.get(i) + 1);
}
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (stu.get(i) != 0)
++ans;
}
return ans;
}
}
태그 - algorithm, hash(사용한 자료구조), boj(플랫폼)