当前位置:网站首页>LeetCode刷题——715. Range 模块
LeetCode刷题——715. Range 模块
2022-06-23 00:34:00 【艾醒】
Range模块是跟踪数字范围的模块。设计一个数据结构来跟踪表示为 半开区间 的范围并查询它们。
半开区间 [left, right) 表示所有 left <= x < right 的实数 x 。
实现 RangeModule 类:
RangeModule() 初始化数据结构的对象。
void addRange(int left, int right) 添加 半开区间 [left, right),跟踪该区间中的每个实数。添加与当前跟踪的数字部分重叠的区间时,应当添加在区间 [left, right) 中尚未跟踪的任何数字到该区间中。
boolean queryRange(int left, int right) 只有在当前正在跟踪区间 [left, right) 中的每一个实数时,才返回 true ,否则返回 false 。
void removeRange(int left, int right) 停止跟踪 半开区间 [left, right) 中当前正在跟踪的每个实数。
示例 1:
输入
[“RangeModule”, “addRange”, “removeRange”, “queryRange”, “queryRange”, “queryRange”]
[[], [10, 20], [14, 16], [10, 14], [13, 15], [16, 17]]
输出
[null, null, null, true, false, true]
解释
RangeModule rangeModule = new RangeModule();
rangeModule.addRange(10, 20);
rangeModule.removeRange(14, 16);
rangeModule.queryRange(10, 14); 返回 true (区间 [10, 14) 中的每个数都正在被跟踪)
rangeModule.queryRange(13, 15); 返回 false(未跟踪区间 [13, 15) 中像 14, 14.03, 14.17 这样的数字)
rangeModule.queryRange(16, 17); 返回 true (尽管执行了删除操作,区间 [16, 17) 中的数字 16 仍然会被跟踪)
提示:
1 <= left < right <= 109
在单个测试用例中,对 addRange 、 queryRange 和 removeRange 的调用总数不超过 104 次
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/range-module
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
from sortedcontainers import SortedDict
class RangeModule:
def __init__(self):
self.intervals = SortedDict()
def addRange(self, left: int, right: int) -> None:
itvs_ = self.intervals
x = itvs_.bisect_right(left)
print(x)
if x != 0:
start = x - 1
if itvs_.values()[start] >= right:
return
if itvs_.values()[start] >= left:
left = itvs_.keys()[start]
itvs_.popitem(start)
x -= 1
while x < len(itvs_) and itvs_.keys()[x] <= right:
right = max(right, itvs_.values()[x])
itvs_.popitem(x)
itvs_[left] = right
def queryRange(self, left: int, right: int) -> bool:
itvs_ = self.intervals
x = itvs_.bisect_right(left)
if x == 0:
return False
return right <= itvs_.values()[x - 1]
def removeRange(self, left: int, right: int) -> None:
itvs_ = self.intervals
x = itvs_.bisect_right(left)
if x != 0:
start = x - 1
if (ri := itvs_.values()[start]) >= right:
if (li := itvs_.keys()[start]) == left:
itvs_.popitem(start)
else:
itvs_[li] = left
if right != ri:
itvs_[right] = ri
return
elif ri > left:
itvs_[itvs_.keys()[start]] = left
while x < len(itvs_) and itvs_.keys()[x] < right:
if itvs_.values()[x] <= right:
itvs_.popitem(x)
else:
itvs_[right] = itvs_.values()[x]
itvs_.popitem(x)
break
边栏推荐
- 14. longest common prefix
- Oracle ASM uses the CP command in asmcmd to perform remote replication
- ROS1Noetic在Win11中安装记录
- Anti shake & throttling enhanced version
- 【GO】Go数组和切片(动态数组)
- Because I said: volatile is a lightweight synchronized, the interviewer asked me to go back and wait for the notice!
- OJ daily practice - class dining
- OJ daily exercise - virus proliferation
- OJ daily practice - word length
- a++,++a,!,~
猜你喜欢
随机推荐
To establish a cloud computing "Kunlun", why should Lenovo hybrid cloud Lenovo xcloud?
OLAP - Druid introduction
Anti shake & throttling enhanced version
打新债开户安全么,怎么开
Typecho imite le modèle de thème du blog Lu songsongsong / modèle de thème du blog d'information technologique
OJ daily practice - sorting and naming
Ecmascript6 new features
After passing the hcip exam, I still failed to change my career. What do professional network workers value most
OJ daily practice - word length
14. longest common prefix
[go] getting started with go modules
数据库每日一题---第20天:按日期分组销售产品
Oracle ASM uses the CP command in asmcmd to perform remote replication
KunlunDB查询优化(二)Project和Filter下推
2022天梯赛-全国总决赛复盘赛
[go] go language interface
图神经网络有哪些用途和应用?
SAP UI5 应用开发教程之一百零三 - 如何在 SAP UI5 应用中消费第三方库
Sword finger offer 06 Print linked list from end to end
华为云招募工业智能领域合作伙伴,强力扶持+商业变现







