当前位置:网站首页>LeetCode 715. Range module
LeetCode 715. Range module
2022-07-03 09:02:00 【Sasakihaise_】

【 Ordered set 】 use TreeMap To merge and split ordered sets
class RangeModule {
// Interval splitting and merging 9:34 10:31
TreeMap<Integer, Integer> map = new TreeMap();
public RangeModule() {
}
public void addRange(int left, int right) {
Integer start = map.floorKey(right);
while(start != null){
int end = map.get(start);
if(end >= left){
left = Math.min(left, start);
right = Math.max(right, end);
map.remove(start);
}else{
break;
}
start = map.floorKey(right);
}
map.put(left, right);
}
public boolean queryRange(int left, int right) {
// for(var k: map.keySet()){
// System.out.print("[" + k + "," + map.get(k) + "],");
// }
// System.out.println();
// Integer start = map.floorKey(right);
// if(start == null) return false;
// if(start > left) return false;
// if(map.get(start) < right) return false;
// return true;
Integer start = map.floorKey(left);
if(start == null) return false;
if(map.get(start) >= right) return true;
return false;
}
public void removeRange(int left, int right) {
//. 3 In this case
// start________end
// left_________right. [start, left], [end, right]X
// start________end
//. left______________right. [left, start]X, [end, right]X
//. start______________end
// left_______right [start, left], [right, end]
Integer start = map.floorKey(right);
while(start != null){
int end = map.get(start);
if(start == right) break;
if(end <= left) break;
else{
map.remove(start);
int s1 = Math.min(start, left);
int e1 = Math.max(start, left);
if(s1 == start && e1 == left && s1 != e1) map.put(s1, e1);
int s2 = Math.min(right, end);
int e2 = Math.max(right, end);
if(s2 == right && e2 == end && s2 != e2) {
map.put(s2, e2);
right --;
}
}
start = map.floorKey(right);
}
}
}
/**
* Your RangeModule object will be instantiated and called as such:
* RangeModule obj = new RangeModule();
* obj.addRange(left,right);
* boolean param_2 = obj.queryRange(left,right);
* obj.removeRange(left,right);
*/边栏推荐
- AcWing 788. 逆序对的数量
- On a un nom en commun, maître XX.
- 高斯消元 AcWing 883. 高斯消元解线性方程组
- First Servlet
- Solution of 300ms delay of mobile phone
- [concurrent programming] concurrent security
- Slice and index of array with data type
- 我们有个共同的名字,XX工
- LeetCode 30. 串联所有单词的子串
- The method for win10 system to enter the control panel is as follows:
猜你喜欢

too many open files解决方案

Alibaba canal actual combat

Annotations simplify configuration and loading at startup

教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?

LeetCode 324. 摆动排序 II

Vscode connect to remote server

LeetCode 30. 串联所有单词的子串

请求参数的发送和接收

Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?

Markdown learning
随机推荐
Really explain the five data structures of redis
传统企业数字化转型需要经过哪几个阶段?
第一个Servlet
String splicing method in shell
剑指 Offer II 091. 粉刷房子
Gif remove blank frame frame number adjustment
求组合数 AcWing 885. 求组合数 I
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
TP5 multi condition sorting
[concurrent programming] atomic operation CAS
Find the combination number acwing 886 Find the combination number II
推荐一个 yyds 的低代码开源项目
Tree DP acwing 285 A dance without a boss
Sending and receiving of request parameters
浅谈企业信息化建设
LeetCode 508. 出现次数最多的子树元素和
Annotations simplify configuration and loading at startup
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
记忆化搜索 AcWing 901. 滑雪
On a un nom en commun, maître XX.