kk
This commit is contained in:
41
src/main/java/com/dota/treeMap/Solution.java
Normal file
41
src/main/java/com/dota/treeMap/Solution.java
Normal file
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user