import java.util.Arrays;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = 0;
int[] clothes = new int[n];
Arrays.fill(clothes, 1);
for (int l : lost) {
clothes[l - 1] = 0;
}
for (int r : reserve) {
clothes[r - 1] = 2;
}
for (int i = 0; i < n; i++) {
if (clothes[i] == 0) {
if (i > 0 && clothes[i - 1] > 1) {
clothes[i]++;
clothes[i - 1]--;
} else if (i < n - 1 && clothes[i + 1] > 1) {
clothes[i]++;
clothes[i + 1]--;
}
}
}
for (int i = 0; i < n; i++) {
if (clothes[i] > 0) {
answer++;
}
}
return answer;
}
}
여벌 체육복을 가져온 학생이 체육복을 도난당했을 수 있습니다. 이때 이 학생은 체육복을 하나만 도난당했다고 가정하며, 남은 체육복이 하나이기에 다른 학생에게는 체육복을 빌려줄 수 없습니다.
이 부분을 빠뜨렸다. 초기에 lost와 reserve배열을 읽을때 0, 2로 세팅하는게 아니라 증감연산자로 이전 상태를 보존해야한다.
import java.util.Arrays;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = 0;
int[] clothes = new int[n];
Arrays.fill(clothes, 1);
for (int l : lost) {
clothes[l - 1]--;
}
for (int r : reserve) {
clothes[r - 1]++;
}
for (int i = 0; i < n; i++) {
if (clothes[i] == 0) {
if (i > 0 && clothes[i - 1] > 1) {
clothes[i]++;
clothes[i - 1]--;
} else if (i < n - 1 && clothes[i + 1] > 1) {
clothes[i]++;
clothes[i + 1]--;
}
}
}
for (int i = 0; i < n; i++) {
if (clothes[i] > 0) {
answer++;
}
}
return answer;
}
}
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int[] people = new int[n];
int answer = n;
for (int l : lost)
people[l-1]--;
for (int r : reserve)
people[r-1]++;
for (int i = 0; i < people.length; i++) {
if(people[i] == -1) {
if(i-1>=0 && people[i-1] == 1) {
people[i]++;
people[i-1]--;
}else if(i+1< people.length && people[i+1] == 1) {
people[i]++;
people[i+1]--;
}else
answer--;
}
}
return answer;
}
}