Skip to main content
replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

You're trying to do too much in one loop. Subtracting the gaps is difficult because it's difficult to identify the gaps. Identifying the gaps is difficult because of all the weird ways the intervals overlap, so remove the overlapping interval problem first.

Make one pass to flatten the intervals, using an algorithm from this questionthis question. Then one pass to make a list of gaps, which should be very simple with the flattened list.

Now you're ready to loop through your original list of intervals. For each interval, see how many gaps occur before it, and subtract the cumulative time of each.

You're trying to do too much in one loop. Subtracting the gaps is difficult because it's difficult to identify the gaps. Identifying the gaps is difficult because of all the weird ways the intervals overlap, so remove the overlapping interval problem first.

Make one pass to flatten the intervals, using an algorithm from this question. Then one pass to make a list of gaps, which should be very simple with the flattened list.

Now you're ready to loop through your original list of intervals. For each interval, see how many gaps occur before it, and subtract the cumulative time of each.

You're trying to do too much in one loop. Subtracting the gaps is difficult because it's difficult to identify the gaps. Identifying the gaps is difficult because of all the weird ways the intervals overlap, so remove the overlapping interval problem first.

Make one pass to flatten the intervals, using an algorithm from this question. Then one pass to make a list of gaps, which should be very simple with the flattened list.

Now you're ready to loop through your original list of intervals. For each interval, see how many gaps occur before it, and subtract the cumulative time of each.

Source Link
Karl Bielefeldt
  • 148.4k
  • 38
  • 284
  • 483

You're trying to do too much in one loop. Subtracting the gaps is difficult because it's difficult to identify the gaps. Identifying the gaps is difficult because of all the weird ways the intervals overlap, so remove the overlapping interval problem first.

Make one pass to flatten the intervals, using an algorithm from this question. Then one pass to make a list of gaps, which should be very simple with the flattened list.

Now you're ready to loop through your original list of intervals. For each interval, see how many gaps occur before it, and subtract the cumulative time of each.