In 14.8, the sample code uses vector and HashSet to check whether an interval has been covered or not. I uses a different method, I created a wrapper class for Interval to record which visiting will cover it. And we only need to check this visiting record to find out the visiting point:
private static class Wrapper
{
final Interval data;
int num;
Wrapper(final Interval item)
{
this.data = item;
num = 0;
}
}
…
// Make sure both share same wrapper instance.
for (final Interval item : intervals)
{
final Wrapper wrapper = new Wrapper(item);
items[num++] = new StartPoint(wrapper);
items[num++] = new EndPoint(wrapper);
}
…
// Visiting time.
num = 0;
for (final Point item : items)
{
// Start point, set the current covering time.
if (item instanceof StartPoint)
{
item.item.num = num;
}
// First end point in this iteration.
else if (item.item.num == num)
{
// Update the cover time, all following element should in next iteration.
result[num++] = item.getValue();
}
}
In 14.10, the code of placing the elements can be improved a little:
- the while condition can be key_to_offset.size() > 1, which means only one key hasn’t been placed completely, because all other keys have been placed completely, the only left one is placed well too. Consider there’s only 1 key available, or 2 keys but one of them only has 1 element, once we placed that element correctly, the job is done.
- we can check the from and to element has same key, in that case we don’t need to swap the two.