当前位置:网站首页>Leetcode question brushing - 715 Range module
Leetcode question brushing - 715 Range module
2022-06-23 00:39:00 【AI Xing】
Range Modules are modules that track the digital range . Design a data structure to trace the representation as Semi open interval And query them .
Semi open interval [left, right) Express all left <= x < right The real number x .
Realization RangeModule class :
RangeModule() The object that initializes the data structure .
void addRange(int left, int right) add to Semi open interval [left, right), Track every real number in the interval . When adding an interval that overlaps the number part of the current trace , Should be added in the interval [left, right) Any number that has not been tracked in to this interval .
boolean queryRange(int left, int right) Only in the currently tracking range [left, right) For each real number in , To return to true , Otherwise return to false .
void removeRange(int left, int right) cease tracking Semi open interval [left, right) Each real number currently being tracked in .
Example 1:
Input
[“RangeModule”, “addRange”, “removeRange”, “queryRange”, “queryRange”, “queryRange”]
[[], [10, 20], [14, 16], [10, 14], [13, 15], [16, 17]]
Output
[null, null, null, true, false, true]
explain
RangeModule rangeModule = new RangeModule();
rangeModule.addRange(10, 20);
rangeModule.removeRange(14, 16);
rangeModule.queryRange(10, 14); return true ( Section [10, 14) Each number in is being tracked )
rangeModule.queryRange(13, 15); return false( Untracked interval [13, 15) As in the 14, 14.03, 14.17 Numbers like this )
rangeModule.queryRange(16, 17); return true ( Despite the deletion , Section [16, 17) Number in 16 Will still be tracked )
Tips :
1 <= left < right <= 109
In a single test case , Yes addRange 、 queryRange and removeRange The total number of calls to does not exceed 104 Time
source : Power button (LeetCode)
link :https://leetcode.cn/problems/range-module
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
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
边栏推荐
- SAP UI5 应用开发教程之一百零二 - SAP UI5 应用的打印(Print)功能实现详解试读版
- 打新债开户安全么,怎么开
- 【UVM】别再说你的 VIP 用不了 RAL Model
- How Huawei cloud implements a global low delay network architecture for real-time audio and video [Part 1]
- 62. different paths
- MGRE环境下的OSPF实验
- cadence SPB17.4 - allegro - 优化指定单条电气线折线连接角度 - 折线转圆弧
- TiDB VS MySQL
- 昆仑分布式数据库技术优势
- 【滑动窗口】leetcode992. Subarrays with K Different Integers
猜你喜欢

数据库中数据的储存结构和方式是什么?

TIDB监控升级解决panic的漫漫探索之路
声网多人视频录制与合成支持掉线再录制 | 掘金技术征文

DCC888 :SSA (static single assignment form)

3dMax建模笔记(一):介绍3dMax和创建第一个模型Hello world

Oracle ASM uses the CP command in asmcmd to perform remote replication

如何入门机器学习?

Introduction to the unique variable reading and writing function of Kunlun distributed database

TiDB VS MySQL

数据库每日一题---第20天:按日期分组销售产品
随机推荐
KunlunDB 查询优化(一)
How Huawei cloud implements a global low delay network architecture for real-time audio and video [Part 1]
关于测试/开发程序员技术的一些思考,水平很高超的,混不下去了......
Why do we not use foreign keys now (2)?
Oracle ASM使用asmcmd中的cp命令来执行远程复制
华为云招募工业智能领域合作伙伴,强力扶持+商业变现
DCC888 :SSA (static single assignment form)
打新债属于什么理财产品?
百度交易中台之钱包系统架构浅析
2022天梯赛-全国总决赛复盘赛
[go] go modules GETTING STARTED
SAP UI5 应用开发教程之一百零三 - 如何在 SAP UI5 应用中消费第三方库试读版
每日刷题记录 (一)
How to set the power-off auto start of easycvr hardware box
二叉树转字符串及字符串转二叉树
BGP联邦综合实验
美团基于 Flink 的实时数仓平台建设新进展
语义分割新范式!StructToken:对per-pixel 分类范式的重新思考
Express、路由(Route)、Request对象、Response对象、中间件、EJS模板
打新债开户安全么,怎么开