728x90
    
    
  https://school.programmers.co.kr/learn/courses/30/lessons/42862?language=javascript
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
function solution(n, lost, reserve) {
    var answer = 0;
    
    lost = lost.map((l) => {
       if(reserve.includes(l)) {
           reserve = new Set(reserve);
           reserve.delete(l);
           reserve = Array.from(reserve);
       } else return l;
    });
    
    let borrow = new Array(n).fill().map(n => 0);
    borrow = borrow.map((n, index) => {
        if(reserve.includes(index+1)) {
            return 1;
        } else return 0;
    });
    
    let student = new Array(n).fill().map(n => true);
    student = student.map((n, index) => {
        if(lost.includes(index+1)) {
            return false;
        } else return true;
    });
    
    student.forEach((s, index) => {
        if(!s) { // 체육복 없는 학생
            let left = index -1 < 0 ? null : index -1;
            let right = index +1 >= student.length ? null : index +1;
            
            if(left !== null && borrow[left] === 1) {
                student[index] = true;
                borrow[left] = 0;
            } else if(borrow[right] !== null && borrow[right] === 1) {
                student[index] = true;
                borrow[right] = 0;            
            }
        }
    })
    return student.filter(s => s === true).length;
}'Algorithm&CodingTest > Programmers' 카테고리의 다른 글
| [Programmers] 연습문제 - 숫자의 표현 (0) | 2023.03.08 | 
|---|---|
| [Programmers] 월간 코드 챌린지 시즌1 - 이진 변환 반복하기 (0) | 2023.03.01 | 
| [Programmers] 코딩테스트 입문 - 분수의 덧셈 (0) | 2023.02.24 | 
| [Programmers] 코딩테스트 입문 - 유한소수 판별하기 (0) | 2023.02.24 | 
| [Programmers] 코딩테스트 입문 - 등수 매기기 (0) | 2023.02.16 |