分享一个比较好的判断区间是否重叠的方法

栗子:比如学校排课表的课时,判断同一天是否存在重叠;

数据举例:A课程:开始课时第1节,结束课时第3节,B课程:开始课时第2节,结束课时第4节。

思路:比较开始课时数组的最大值和结束课时数组的最小值,若小于或等于则重叠,反之不重叠,也就是没有交集或包含的情况。

 1 const AStart = 1, AEnd = 3, BStart = 2,  BEnd = 4;
 2 
 3 // 开始课时数组
 4 const maxStart = [AStart, BStart]; 
 5 // 结束课时数组
 6 const minEnd = [AEnd, BEnd];
 7 
 8 if(Math.max(...maxStart)<= Math.min(...minEnd)) {
 9     console.log("重叠了");
10 }

 

相关文章:

  • 2022-12-23
  • 2021-08-08
  • 2022-02-06
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
  • 2021-06-24
  • 2021-07-11
猜你喜欢
  • 2021-07-24
  • 2021-05-03
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2021-11-08
相关资源
相似解决方案