会议室2 Leetcode问题在JavaScript中

我们将得到一个数组数组,每个子数组都由两个元素组成,分别指示会议的开始和结束时间。

我们职能的任务是找到一个人最多可以开会的次数,以避免时间冲突。该函数最终应返回该数字。

例如-

如果描述会议时间的输入数组为-

const arr = [[5, 40], [10, 20], [25, 35]];

那么输出应该是-

const output = 2;

因为由于时间重叠而无法全部参加三个会议,但可以参加[10,20]和[25,35]。

示例

为此的代码将是-

const arr = [[5, 40], [10, 20], [25, 35]];
const canAttendAll = (arr = []) => {
   const times = new Set();
   const { length } = arr;
   for (let i = 0; i < length; i += 1) {
      for (let j = arr[i][0]; j < arr[i][1]; j += 1) {
         if (times.has(j)) {
            return false;
         } else {
            times.add(j);
         };
      };
   };
   return true;
};
console.log(canAttendAll(arr));
输出结果

控制台中的输出将是-

false