当前位置:网站首页>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);
*/
边栏推荐
- Low code momentum, this information management system development artifact, you deserve it!
- LeetCode 715. Range 模块
- How to delete CSDN after sending a wrong blog? How to operate quickly
- Get the link behind? Parameter value after question mark
- PHP function date (), y-m-d h:i:s in English case
- Using variables in sed command
- 网络安全必会的基础知识
- Debug debugging - Visual Studio 2022
- 拯救剧荒,程序员最爱看的高分美剧TOP10
- TP5 multi condition sorting
猜你喜欢
DOM render mount patch responsive system
Es8 async and await learning notes
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Parameters of convolutional neural network
LeetCode 871. 最低加油次数
请求参数的发送和接收
Allocation exception Servlet
Wonderful review | i/o extended 2022 activity dry goods sharing
Deep parsing (picture and text) JVM garbage collector (II)
随机推荐
Common penetration test range
MySQL index types B-tree and hash
Too many open files solution
Facial expression recognition based on pytorch convolution -- graduation project
Phpstudy 80 port occupied W10 system
TP5 multi condition sorting
Sending and receiving of request parameters
Complex character + number pyramid
[rust note] 10 operator overloading
Analysis of Alibaba canal principle
数位统计DP AcWing 338. 计数问题
cres
What is the difference between sudo apt install and sudo apt -get install?
Binary to decimal, decimal to binary
拯救剧荒,程序员最爱看的高分美剧TOP10
Shell script kills the process according to the port number
Life cycle of Servlet
The difference between if -n and -z in shell
求组合数 AcWing 886. 求组合数 II
Methods of using arrays as function parameters in shell