スポーツウェア


質問する
100/100
1時間以上
元のloss reserveでは
つなぎ合わせを使ってみましたが.
lost.mapのlossをつなぎ合わせると、巡回時の結果は想像しにくい.
考え方は、新しいレイアウトで各オブジェクトを1つのオブジェクトにすることです.
私が書いたコード
function solution(n, lost, reserve) {
  // 체육복을 빌리는 사람과 잃어버린 사람간의 객체로 값을 가진 배열 생성
  const reserveCheck = reserve
    .sort((a, b) => a - b)
    .map((reserveStudent) => {
      return { isBorrowed: false, borrowStudentIdx: reserveStudent };
    });
  const lostCheck = lost
    .sort((a, b) => a - b)
    .map((lostStudent) => {
      return { isBorrowed: false, borrowStudentIdx: lostStudent };
    });

  // 여분이 있는 학생이 도난을 당했을때
  lostCheck.map((lostStudent, idx) => {
    if (reserve.indexOf(lostStudent.borrowStudentIdx) > -1) {
      lostCheck[idx].isBorrowed = true;
      reserveCheck[
        reserve.indexOf(lostStudent.borrowStudentIdx)
      ].isBorrowed = true;
    }
  });

  // 여분이 없는 학생이 도난을 당했을때
  lostCheck.map((lostStudent, idx) => {
    if (!lostStudent.isBorrowed) {
      //앞번호 학생이 여분을 가지고 있고 아직 빌리지 않았을 때
      if (
        reserve.indexOf(lostStudent.borrowStudentIdx - 1) > -1 &&
        !reserveCheck[reserve.indexOf(lostStudent.borrowStudentIdx - 1)]
          .isBorrowed
      ) {
        lostCheck[idx].isBorrowed = true;
        reserveCheck[
          reserve.indexOf(lostStudent.borrowStudentIdx - 1)
        ].isBorrowed = true;
      }
      //뒷번호 학생이 여분을 가지고 있고 아직 빌리지 않았을 때
      else if (
        reserve.indexOf(lostStudent.borrowStudentIdx + 1) > -1 &&
        !reserveCheck[reserve.indexOf(lostStudent.borrowStudentIdx + 1)]
          .isBorrowed
      ) {
        lostCheck[idx].isBorrowed = true;
        reserveCheck[
          reserve.indexOf(lostStudent.borrowStudentIdx + 1)
        ].isBorrowed = true;
      }
    }
  });

  let borrowCount = 0;
  lostCheck.map((lostStudent) => {
    if (lostStudent.isBorrowed) borrowCount++;
  });
  return n - lost.length + borrowCount;
}