회의실 배정

WooBuntu·2021년 3월 30일
0

JS 90제

목록 보기
33/33
  • 강의 듣기 전 풀이
function sort(coordinates) {
  return coordinates.sort(([xOfFormer, yOfFormer], [xOfLatter, yOfLatter]) => {
    if (xOfFormer == xOfLatter) {
      return yOfFormer < yOfLatter ? -1 : 1;
    }
    return xOfFormer < xOfLatter ? -1 : 1;
  });
}

function solution(arr) {
  let max = 0;
  arr = sort(arr);
  for (let i = 0; i < arr.length - 1; i++) {
    let end = arr[i][1];
    let count = 1;
    for (let j = i + 1; j < arr.length; j++) {
      const [curStart, curEnd] = arr[j];
      if (curStart >= end) {
        end = curEnd;
        count++;
      }
    }
    if (count > max) max = count;
  }
  return max;
}

// const result = solution([
//   [1, 4],
//   [2, 3],
//   [3, 5],
//   [4, 6],
//   [5, 7],
// ]);

const result = solution([
  [3, 3],
  [1, 3],
  [2, 3],
]);

console.log(result);
  • 강의 반영한 풀이
function sort(coordinates) {
  return coordinates.sort((former, latter) =>
    former[1] == latter[1] ? former[0] - latter[0] : former[1] - latter[1],
  );
}

function solution(arr) {
  arr = sort(arr);
  console.log(arr);
  let end = 0;
  let count = 0;
  for (let i = 0; i < arr.length; i++) {
    const [curStart, curEnd] = arr[i];
    if (curStart >= end) {
      end = curEnd;
      count++;
    }
  }
  return count;
}

// const result = solution([
//   [1, 4],
//   [2, 3],
//   [3, 5],
//   [4, 6],
//   [5, 7],
// ]);

const result = solution([
  [3, 3],
  [1, 3],
  [2, 3],
]);

console.log(result);

일찍 끝나는 순으로 정렬...

0개의 댓글