Brush_algorithm icon indicating copy to clipboard operation
Brush_algorithm copied to clipboard

1229 安排会议日程 | 中等级-算法

Open OBKoro1 opened this issue 5 years ago • 0 comments

博客链接

# 1229 安排会议日程

# 题目链接

# 难度:中等

# 思路分析:

滑窗思想,一端一端固定

#

#

#

#

#

#

#

#

#

#

#

#

#

#

# 代码:

/**
 * @param {number[][]} slots1
 * @param {number[][]} slots2
 * @param {number} duration
 * @return {number[]}
 */
// 滑窗 划出范围
var minAvailableDuration = function(slots1, slots2, duration) {
  let start = 0,
    end = 0,
    find = false;
  // 排序找到最早的时间
  const fn = (a, b) => {
    return a[0] - b[0];
  };
  slots1.sort(fn);
  slots2.sort(fn);
  for (let i = 0; i < slots1.length; i++) {
    const item1 = slots1[i];
    for (let j = 0; j < slots2.length; j++) {
      const item2 = slots2[j];
      if (item1[1] < item2[0]) {
        continue;
      }
      if (item2[1] < item1[0]) {
        continue;
      }
      // 滑窗思想 先固定一边 再固定另一边
      start = Math.max(item1[0], item2[0]); // 找到最小值中最大的
      end = Math.min(item1[1], item2[1]); // 找到最大值最小的
      // 测试区间范围
      if (end - start >= duration) {
        find = true;
        break;
      }
    }
    if (find) {
      break;
    }
  }
  if (find) {
    end = start + duration; // 最早会议时间
    return [start, end];
  }
  return [];
};

# 鼓励我一下:

觉得还不错的话,给我的项目点个star

# 点个Star支持我一下~

博客链接

OBKoro1 avatar Jul 02 '20 08:07 OBKoro1