-1

I have a set of data about busy times during day and I want to actually predict the unbusy time somehow by probably subtracting those time slots from the day.

I'm wondering about what's the best approach/algorithm to do that.

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

what I'm thinking about right now is to make an array with 24 blocks of the day as initially free [1-2, 2-3, 3-4, 4-5 ..etc] and somehow process the starting time and deduct it from that spot ex. 1.30 to 2.00 would turn the free block of 1 from 1-2 to 1-1.30. This is something to start with but not sure if it will eventually work

Jack Dre
  • 1,026
  • 2
  • 9
  • 15
  • Do you have any thoughts? "what's the best approach/algorithm" --- do you have at least anything working that you consider as "not the best"? – zerkms Oct 06 '13 at 22:05
  • I'm not sure what are you asking for. You can make an unbusy time template, like `"9:00 to 19:00"`, then make an [intersection](http://en.wikipedia.org/wiki/Intersection_%28set_theory%29) of free time and busy time. After this, you just need to convert the data to some numbers. – Tomáš Zato Oct 06 '13 at 22:09
  • You can divide a full day (24h) into an array of numbers representing 5 minute slots, then for each string in your data increase the counter in the corresponding slots. Last, sort the array. You're looking for the slots with the lowest number. – Šime Vidas Oct 06 '13 at 22:13
  • @zerkms you know sometimes people are totally clueless where to start from, that's why they ask. – Jack Dre Oct 06 '13 at 22:17
  • @TomášZato what I'm thinking about right now is to make an array with 24 blocks of the day as initially free [1-2, 2-3, 3-4, 4-5 ..etc] and somehow process the starting time and deduct it from that spot ex. 1.30 to 2.00 would turn the free block of 1 from 1-2 to 1-1.30. This is something to start with but not sure if it will eventually work. – Jack Dre Oct 06 '13 at 22:18
  • @ŠimeVidas thank you so much. can you please elaborate a little bit more? – Jack Dre Oct 06 '13 at 22:19
  • 1
    @Jack Dre: how would you do that if you were asked to do it manually on a piece of paper? – zerkms Oct 06 '13 at 22:24
  • @JackDre What part don't you understand? – Šime Vidas Oct 06 '13 at 23:25

2 Answers2

1

I had a similar problem, I had the busy slots of a person, and wanted to find the time slots for which that person was available ("free"). This is what I coded, hope it can help someone:

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;
linaa
  • 199
  • 2
  • 10
0

What I would do is to store the boundaries between busy time and working time in an array of Date objects containing the actual boundaries (and an initial condition, of course). To parse your current Date data, you may want to take a look at Why does Date.parse give incorrect results?.

Then, to decide whether some slot is free or busy, just do a binary search on your boundaries array and decide based on the parity of the position returned by the search.

Community
  • 1
  • 1
naitoon
  • 665
  • 5
  • 14