3
// for storing intervals4
TreeMap<Integer, Integer> map = new TreeMap<>();7
public CountIntervals() {}9
public void add(int left, int right) {10
// we are taking left side as reference and calculating ans12
// this is the current length13
int add = right - left + 1;15
// var is the default data type16
// lowerentry gives the value of smaller(smaller largest) entry compared to (left+1)17
// we added +1 bcz there may be a case where left is present in a map18
// lowerentry gives the value(that is)<left+119
var left_bound = map.lowerEntry(left + 1);21
// if there is some value present then go22
// getvalue gives the right positon23
// if current left is cover under map.right value means there is a overlap24
if (left_bound != null && left_bound.getValue() >= left) {25
// positions are map.left <= left <= min(map.right,right) <= max(map.right,right)27
// examples: (1) (2) (3)28
// ____________ _________ _______________ (map)29
// ___________ ________ ____ (our)31
// now, left become the smallest value which in this case is map.left32
left = left_bound.getKey();34
// same as right become the maximum value35
right = Math.max(left_bound.getValue(), right);37
// value overlap means curr answer(add) also change38
// we already calculated the map value (covered area) and added into ans variable39
// calculate/add the new uncovered value41
// max(right,map.right) - map.right42
// we are not adding +1 bcz that is already handled by overlaping area43
add = right - left_bound.getValue();45
// this is taken so we have to remove46
// at last we added the largest area (which is stored int left,right)47
map.remove(left_bound.getKey());50
// check is right overlaping51
// if yes then take the correct values52
// higherEntry gives the largest(largest smallest) value in map53
// we are not taking left+1 bcz that condition is already been satisfied54
// eg. left=5 map contains (5,value)55
// condition is checked in left_bound56
// i.getKey()<=right means curr is beneath our map.left and covering area(overlaping)57
for (var i = map.higherEntry(left);58
i != null && i.getKey() <= right;59
i = map.higherEntry(left)) {60
// left <= map.left <= min(right,map.right) <= max(right,map.right)62
// examples: (1) (2) (3)63
// ____________ _________ _______ (map)64
// ___________ ________ _________________ (our)66
// now we have our add which is current ans but some area is overlaping67
// so we have to subtract overlaping area68
// +1 bcz [2,5] we have 4 position (2,3,4,5) ; 5-2=>3 ; we ned to add 169
add -= Math.min(right, i.getValue()) - i.getKey() + 1;71
// right value become the largest among them72
right = Math.max(right, i.getValue());74
// we have taken care this entry and calculated left and right now we don't need75
map.remove(i.getKey());78
// we pushed the values79
// this entry can be essential for next calls82
// add the current ans92
* Your CountIntervals object will be instantiated and called as such: CountIntervals obj = new93
* CountIntervals(); obj.add(left,right); int param_2 = obj.count();