当前位置:网站首页>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);
*/边栏推荐
- TP5 multi condition sorting
- file_ put_ contents
- AcWing 787. 归并排序(模板)
- 数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
- Monotonic stack -84 The largest rectangle in the histogram
- Markdown learning
- Parameters of convolutional neural network
- Es8 async and await learning notes
- [concurrent programming] explicit lock and AQS
- On the difference and connection between find and select in TP5 framework
猜你喜欢

Annotations simplify configuration and loading at startup

20220630 learning clock in

网络安全必会的基础知识

Mortgage Calculator

How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework

推荐一个 yyds 的低代码开源项目

LeetCode 57. 插入区间

TP5 multi condition sorting

Tree DP acwing 285 A dance without a boss

剑指 Offer II 091. 粉刷房子
随机推荐
LeetCode 1089. 复写零
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
DOM render mount patch responsive system
Allocation exception Servlet
Log4j2 vulnerability recurrence and analysis
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
即时通讯IM,是时代进步的逆流?看看JNPF怎么说
Deep parsing (picture and text) JVM garbage collector (II)
Divide candy (circular queue)
In the digital transformation, what problems will occur in enterprise equipment management? Jnpf may be the "optimal solution"
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
JS non Boolean operation - learning notes
Try to reprint an article about CSDN reprint
我們有個共同的名字,XX工
Tree DP acwing 285 A dance without a boss
Servlet的生命周期
[rust notes] 09- special types and generics
C language file reading and writing