2013-10-06 17 views
-1

我有一组关于白天繁忙时间的数据,我想实际上可能减去一天,在这些时隙莫名其妙预测不忙的时间。繁忙时间转换为免费时间

我想了解一下什么是最好的方法/算法来做到这一点。

[ '20:30 to 21:30', 
    '11:00 to 12:00', 
    '07:30 to 08:50', 
    '09:00 to 20:00' ] 

什么,我想现在的问题是,以与当天的24块数组作为最初免费[1-2,2-3,3-4,4-5 ..等]及以某种方式处理开始时间,并从该地点扣除它。 1.30到2.00会将1的空闲块从1-2变为1-1.30。这是值得入手,但不知道它最终会工作

+0

你有什么想法吗? “什么是最好的方法/算法”---你至少有什么工作,你认为是“不是最好的”? – zerkms

+0

我不确定你在问什么。你可以创建一个不繁忙的时间模板,比如“9:00-19:00”,然后制作一个空闲时间的交集(http://en.wikipedia.org/wiki/Intersection_%28set_theory%29)忙碌的时间。在此之后,你只需要将数据转换为一些数字。 –

+0

您可以将一整天(24小时)分成代表5分钟时段的数字数组,然后对于数据中的每个字符串增加相应时段中的计数器。最后,对数组进行排序。您正在寻找最低号码的插槽。 –

回答

0

我会做什么是繁忙时间和工作时间之间的界限存储在包含实际边界日期对象的数组(和初始条件,课程)。要解析您当前的日期数据,您可能需要查看Why does Date.parse give incorrect results?

然后,来决定一些插槽是空闲还是忙碌,只是做你的界限阵列上的二进制搜索和基于搜索返回的位置的奇偶决定。

1

我有一个类似的问题,我只好一个人忙插槽,想找个时间插槽,该人是可用的(“自由”)。这是我编码,希望它可以帮助别人:

function getFreeOfDay (date, busySlots) { 
    function sameDateDifferentTime(date, hours, minutes) { 
    return new Date(date.getFullYear(), date.getMonth(), date.getDate(), hours, minutes, 0, 0); 
    } 
    // Define the range for free spots 
    var freeSlots = date.getDay() === 0 || date.getDay() === 6 ? [] : [ 
    { 
     start: sameDateDifferentTime(date, 10, 0), // 10:00 (AM) 
     end: sameDateDifferentTime(date, 12, 30), // 12:30 (AM) 
    }, 
    { 
     start: sameDateDifferentTime(date, 13, 30), // 13:30 (AM) 
     end: sameDateDifferentTime(date, 19, 0), // 19:00 (AM) 
    } 
    ]; 

    // Go through the busy slots, to remove them from the free spots 
    busySlots.forEach(function (busySlot) { 
    freeSlots.forEach(function (freeSlot, freeSlotIndex) { 
     if (busySlot.end <= freeSlot.start || busySlot.start >= freeSlot.end) { 
     // Do nothing, the busy slot doesn't interfere with the free slot 
     } 
     else if (busySlot.start <= freeSlot.start && busySlot.end >= freeSlot.end) { 
     // The free slot is in the middle of the busy slot, meaning it's not possible to plan anything in there 
     freeSlots.splice(freeSlotIndex, 1); 
     } 
     else if (busySlot.start < freeSlot.start && busySlot.end > freeSlot.start) { 
     // The busy slot overlaps with the free slot, it ends after the start of the free slot 
     freeSlots[freeSlotIndex] = { 
      start: busySlot.end, 
      end: freeSlot.end 
     }; 
     } 
     else if (busySlot.start < freeSlot.end && busySlot.end > freeSlot.end) { 
     // The busy slot overlaps with the free slot, it starts before the end of the free slot 
     freeSlots[freeSlotIndex] = { 
      start: freeSlot.start, 
      end: busySlot.start 
     }; 
     } 
     else { 
     // Then the busy slot is in the middle of a free slot 
     freeSlots[freeSlotIndex] = { 
      start: freeSlot.start, 
      end: busySlot.start 
     }; 
     freeSlots.splice(freeSlotIndex + 1, 0, { 
      start: busySlot.end, 
      end: freeSlot.end 
     }); 
     } 
    }); 
    }); 

    // Remove empty free slots 
    freeSlots.forEach(function (freeSlot, freeSlotIndex) { 
    if (freeSlot.start >= freeSlot.end) { 
     freeSlots.splice(freeSlotIndex, 1); 
    } 
    }); 

    return freeSlots;