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 ...
随机推荐
- 我的Android进阶之旅------>Android如何通过自定义SeekBar来实现视频播放进度条
首先来看一下效果图,如下所示: 其中进度条如下: 接下来说一说我的思路,上面的进度拖动条有自定义的Thumb,在Thumb正上方有一个PopupWindow窗口,窗口里面显示当前的播放时间.在Seek ...
- struts 与 Java Web应用简介
struts实质上就是JSP Model2的基础上实现的MVC框架. 在Struts框架中,模型有实现业务逻辑的JavaBean或EJB组件构成 视图由一组JSP文件构成. 控制器 控制器由Actio ...
- spring 获取bean的几种方式
1.读取xml文件的方式,这种在初学入门的时候比较适用 . ApplicationContext applicationContext = new ClassPathXmlApplicationCon ...
- HTML——input
一个简单的HTML表单,包含两个文本输出框和一个提交按钮: <form action="form_action.asp" method="get"> ...
- SpringBoot学习笔记(12):计划任务
SpringBoot学习笔记(12):计划任务 计划任务 在企业的实践生产中,可能需要使用一些定时任务,如月末.季末和年末需要统计各种各样的报表,每周自动备份数据等. 在Spring中使用定时任务 1 ...
- elasticsearch 简单聚合查询示例
因为懒癌犯了,查询语句使用的截图而不是文字,导致了发布随笔的时候提示少于150字的随笔不能发布. 我就很郁闷了. 下面的查询都是前段时间工作中使用过的查询语句. 开始的时候是使用nodejs构建es查 ...
- 学习使用ExpressJS 4.0中的新Router
概述 ExpressJS 4.0中提出了新的路由Router.Router好比是一个"迷你版"的express应用,它没有引入views或者settings,但是提供了路由应有的A ...
- 最全面的HashMap和HashTable的区别
找工作期间不少企业都会问到有关HashMap和HashTable两者直接的区别,很多博客里虽然有提及但总是没有那么全面,只是一些常用的不同,现在就我自己所总结的比较全面的不同,归纳以下: HashMa ...
- ASP.NET MVC3中的路由系统
MVC中,用户访问的地址并不映射到服务器中对应的文件,而是映射到对应Control里对应的ActionMethod,由ActionMethod来决定返回用户什么样的信息.而把用户访问的地址对应到对应的 ...
- BZOJ 1207 [HNOI2004]打鼹鼠:dp【类似最长上升子序列】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1207 题意: 有一个n*n的网格,接下来一段时间内会有m只鼹鼠出现. 第i只鼹鼠会在tim ...