탐욕법(Greedy)문제
https://programmers.co.kr/learn/courses/30/lessons/42862
stu = [1] * (n+2)
for i in lost:
stu[i] -= 1
for i in reserve:
stu[i] += 1
for i in range(1,len(stu)):
if stu[i] == 0 and stu[i-1] == 2 :
stu[i-1] -= 1
stu[i] += 1
elif stu[i] == 0 and stu[i+1] == 2 :
stu[i+1] -= 1
stu[i] += 1
def solution(n, lost, reserve):
stu = [1]*(n+2)
for i in lost:
stu[i] -= 1
for i in reserve:
stu[i] += 1
for i in range(1,len(stu)):
if stu[i] == 0 and stu[i-1] == 2 :
stu[i-1] -= 1
stu[i] += 1
elif stu[i] == 0 and stu[i+1] == 2 :
stu[i+1] -= 1
stu[i] += 1
result = [x for x in stu[1:-1] if x!=0]
return len(result)
function solution(n, lost, reserve) {
let stu = [];
for(let i=0; i<n+2; i++){
stu.push(1);
}
for(let i=0; i<lost.length; i++){
stu[lost[i]] -= 1;
}
for(let i=0; i<reserve.length; i++){
stu[reserve[i]] += 1;
}
for(let i=1; i<stu.length-1; i++){
if(stu[i]==0 && stu[i-1]==2){
stu[i-1] -= 1;
stu[i] += 1;
}else if(stu[i]==0 && stu[i+1]==2){
stu[i+1] -= 1;
stu[i] += 1;
}
}
let result = stu.slice(1, n+1);
return result.filter(x => x!==0).length;
}