To improve file transfer speed, a file is split up into different portions and sent from multiple servers. The receiver downloads the file segments and recombine parts into the single file requested. Given the segment class
class Segment {
int start;
int end;
}implement task class for download manager
class DownloadTask {
// add a segment into recombined file
public void add(Segment segment) {}
// find total length of downloaded segments
public int getFileSize() {}
}Segments sent from different servers are not fully integrated. As a result segments can be overlapping, like [1, 3] and [2, 4] should be merged and considered as [1, 4] with a total length 3 in getFileSize().
Assume that the file is large and the task streams a lot of segments. Please make sure the add() function is efficient.
Example:
DownloadTask task = new DownloadTask();
task.add([1, 5]);
task.add([4, 6]);
task.getFileSize(); // return 5
task.add([10, 20]);
task.add([25, 30]);
task.getFileSize(); // return 20
task.add([19, 29]);
task.getFileSize(); // return 25