当前位置:网站首页>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);
*/边栏推荐
- too many open files解决方案
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
- Parameters of convolutional neural network
- C language file reading and writing
- 浅谈企业信息化建设
- Tree DP acwing 285 A dance without a boss
- Facial expression recognition based on pytorch convolution -- graduation project
- Discussion on enterprise informatization construction
- Final review of Database Principles
- XPath实现XML文档的查询
猜你喜欢
随机推荐
AcWing 785. 快速排序(模板)
AcWing 788. 逆序对的数量
LeetCode 1089. 复写零
How to use Jupiter notebook
Deep parsing (picture and text) JVM garbage collector (II)
Binary tree sorting (C language, char type)
Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
Methods of checking ports according to processes and checking processes according to ports
createjs easeljs
Shell script kills the process according to the port number
Binary to decimal, decimal to binary
Concurrent programming (VI) ABA problems and solutions under CAS
[rust notes] 07 structure
Methods of using arrays as function parameters in shell
[concurrent programming] collaboration between threads
樹形DP AcWing 285. 沒有上司的舞會
[rust notes] 02 ownership
too many open files解决方案
LeetCode 715. Range 模块
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了









