Files
algorithm/src/main/java/com/dota/treeMap/Solution.java

42 lines
714 B
Java
Raw Normal View History

2023-12-16 14:19:36 +08:00
package com.dota.treeMap;
import java.util.Map;
import java.util.TreeMap;
public class Solution {
}
/**
* 2276. 统计区间中的整数数目
*/
class CountIntervals {
TreeMap<Integer, Integer> map = new TreeMap<>();
int cnt = 0;
public CountIntervals() {
}
public void add(int left, int right) {
Map.Entry<Integer, Integer> inte = map.floorEntry(right);
while (inte != null && inte.getValue() >= left) {
int l = inte.getKey();
int r = inte.getValue();
left = Math.min(l, left);
right = Math.max(r, right);
cnt -= r - l + 1;
map.remove(l);
inte = map.floorEntry(right);
}
cnt += (right - left + 1);
map.put(left, right);
}
public int count() {
return cnt;
}
}