715. Range Module
A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.
addRange(int left, int right)
Adds the half-open interval[left, right)
, tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval[left, right)
that are not already tracked.
queryRange(int left, int right)
Returns true if and only if every real number in the interval[left, right)
is currently being tracked.
removeRange(int left, int right)
Stops tracking every real number currently being tracked in the interval[left, right)
.
Example 1:
addRange(10, 20): null
removeRange(14, 16): null
queryRange(10, 14): true (Every number in [10, 14) is being tracked)
queryRange(13, 15): false (Numbers like 14, 14.03, 14.17 in [13, 15) are not being tracked)
queryRange(16, 17): true (The number 16 in [16, 17) is still being tracked, despite the remove operation)
Note:
- A half open interval
[left, right)
denotes all real numbersleft <= x < right
. 0 < left < right < 10^9
in all calls toaddRange, queryRange, removeRange
.- The total number of calls to
addRange
in a single test case is at most1000
. - The total number of calls to
queryRange
in a single test case is at most5000
. - The total number of calls to
removeRange
in a single test case is at most1000
.
Approach #1: C++. [Using Vector]
class RangeModule {
public:
RangeModule() { } void addRange(int left, int right) {
vector<pair<int, int>> new_ranges;
bool inserted = false; for (const auto& it : ranges_) {
if (it.first > right && !inserted) {
new_ranges.emplace_back(left, right);
inserted = true;
}
if (it.first > right || it.second < left) {
new_ranges.push_back(it);
} else {
left = min(left, it.first);
right = max(right, it.second);
}
}
if (!inserted) new_ranges.emplace_back(left, right);
ranges_.swap(new_ranges);
} bool queryRange(int left, int right) {
int l = 0;
int r = ranges_.size() - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (ranges_[mid].first <= left && ranges_[mid].second >= right)
return true;
else if (ranges_[mid].first > right) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return false;
} void removeRange(int left, int right) {
vector<pair<int, int>> new_ranges;
for (const auto& it : ranges_) {
if (it.second <= left || it.first >= right) {
new_ranges.emplace_back(it);
} else {
if (it.first < left)
new_ranges.emplace_back(it.first, left);
if (it.second > right)
new_ranges.emplace_back(right, it.second);
}
}
ranges_.swap(new_ranges);
} private:
vector<pair<int, int>> ranges_;
}; /**
* Your RangeModule object will be instantiated and called as such:
* RangeModule obj = new RangeModule();
* obj.addRange(left,right);
* bool param_2 = obj.queryRange(left,right);
* obj.removeRange(left,right);
*/
there are some notes about STL.
1. the difference between emplace_back and push_back.
3. the method of swap in vector.
Approach #2: C++. [map]
class RangeModule {
public:
RangeModule() { } void addRange(int left, int right) {
IT l, r;
getOverLapRanges(left, right, l, r); if (l != r) {
auto last = r; last--;
left = min(left, l->first);
right = max(right, last->second);
ranges_.erase(l, r);
}
ranges_[left] = right;
} bool queryRange(int left, int right) {
IT l, r;
getOverLapRanges(left, right, l, r);
if (l == r) return false;
return l->first <= left && l->second >= right;
} void removeRange(int left, int right) {
IT l, r;
getOverLapRanges(left, right, l, r);
if (l == r) return ;
auto last = r; last--;
int start = min(left, l->first);
int end = max(right, last->second);
ranges_.erase(l, r);
if (start < left) ranges_[start] = left;
if (end > right) ranges_[right] = end;
} private:
typedef map<int, int>::iterator IT;
map<int, int> ranges_;
void getOverLapRanges(int left, int right, IT& l, IT& r) {
l = ranges_.upper_bound(left);
r = ranges_.upper_bound(right); // judge the left is the leftmost interval?
if (l != ranges_.begin()) {
if ((--l)->second < left) l++;
}
}
}; /**
* Your RangeModule object will be instantiated and called as such:
* RangeModule obj = new RangeModule();
* obj.addRange(left,right);
* bool param_2 = obj.queryRange(left,right);
* obj.removeRange(left,right);
*/
Notes:
715. Range Module的更多相关文章
- Range Module
2019-09-21 18:54:16 715. Range Module 问题描述: 问题求解: 用线段树解决了. class RangeModule { Node root; class Node ...
- [LeetCode] Range Module 范围模块
A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...
- [Swift]LeetCode715. Range 模块 | Range Module
A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...
- Java实现 LeetCode 715 Range 模块(选范围)
715. Range 模块 Range 模块是跟踪数字范围的模块.你的任务是以一种有效的方式设计和实现以下接口. addRange(int left, int right) 添加半开区间 [left, ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
随机推荐
- js实现随机选取[10,100)中的10个整数,存入一个数组,并排序。 另考虑(10,100]和[10,100]两种情况。
1.js实现随机选取[10,100)中的10个整数,存入一个数组,并排序. <!DOCTYPE html> <html lang="en"> <hea ...
- Android数据格式化
1.文件大小格式化: Log.d(TAG, Formatter.formatFileSize(this, 100)); //100 B Log.d(TAG, Formatter.formatFileS ...
- 剑指Offer:把数组排成最小的数【45】
剑指Offer:把数组排成最小的数[45] 题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如,输入数组是{3.32.321},则打印出来的这3 ...
- nokogiri
Nokogiri的用法我推荐三篇非常给力的文章:http://ruby.bastardsbook.com/chapters/html-parsing/http://ruby.bastardsbook. ...
- Appium移动自动化配置-ios&安卓
官网安装参考:https://www.npmjs.com/package/appium Appium安装 1.安装nodejs 2.安装appium iOS侧环境安装 1.安装xcode 2.安装Xc ...
- <再看TCP/IP第一卷>关于链路层的知识细节及相关协议
在TCP/IP协议族中,链路层的主要有三个目的: (1)为IP模块发送和接受数据报 (2)为ARP模块发送ARP请求和接受ARP应答 (3)为RARP发送RARP请求和接受RARP应答 TCP/IP支 ...
- Cocos2d-x中锚点的介绍
什么是锚点? 只需要记住一句话就可以,锚点就是你指定的那个坐标究竟是图像的哪个点,也就是你setPosition的坐标 eg: 新建工程:在HelloWorld中写上如下代码: CCSprite * ...
- BZOJ 1208 [HNOI2004]宠物收养所:Splay(伸展树)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 题意: 有一个宠物收养所,在接下来一段时间内会陆续有一些宠物进到店里,或是一些人来领 ...
- html5--1.15 style元素与HTML样式基础
html5--1.15 style元素与HTML样式基础 学习要点: 1.引入样式的三种方式2.了解style元素插入内联样式表与内部样式表 1.引入样式的三种方式 1.外部样式表:通过 link元素 ...
- web项目的实质
web项目的实质 web项目的实质其实也就是在用户发过来一个请求后,我们返回一个响应. 用户看到的页面就是我们响应中的响应体(里面是html代码). 所以,我们整个项目的所有操作都是围绕着怎么来写好这 ...