当前位置:网站首页>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
边栏推荐
- MySQL-Seconds_behind_master 的精度误差
- How to solve the problem that easycvr does not display the interface when RTMP streaming is used?
- Why do we not use foreign keys now (2)?
- 在一条DML语句中插入/更新/删除/获取几百万行数据,你会特别注意什么?
- What financial product does the new bond belong to?
- ECMAScript6新特性
- LINQ query
- 华为云招募工业智能领域合作伙伴,强力扶持+商业变现
- Notes on zhouguohua's reading
- 【UVM】别再说你的 VIP 用不了 RAL Model
猜你喜欢

SAP UI5 应用开发教程之一百零三 - 如何在 SAP UI5 应用中消费第三方库

华为云如何实现实时音视频全球低时延网络架构【上】

KunlunDB备份和恢复

OpenCvSharp (C# OpenCV) 微信QRCode解码功能使用介绍(附源码)

SAP MM ME27 创建公司内STO单

昆仑分布式数据库独特的变量读写功能介绍

昆仑分布式数据库Sequence功能及其实现机制
Because I said: volatile is a lightweight synchronized, the interviewer asked me to go back and wait for the notice!

Kunlundb query optimization (I)

Quelle est la structure et la façon dont les données sont stockées dans la base de données?
随机推荐
【UVM】别再说你的 VIP 用不了 RAL Model
华为云如何实现实时音视频全球低时延网络架构【上】
“听觉”营销价值凸显,喜马拉雅迎来新局点
EasyCVR使用RTMP推流时不显示界面如何解决?
包管理工具--NPM、--CNPM、 --Yarn、 --CYarn
How Huawei cloud implements a global low delay network architecture for real-time audio and video [Part 1]
3dMax建模笔记(一):介绍3dMax和创建第一个模型Hello world
初学者如何快速入门深度学习?
SAP mm me27 create intra company sto order
美团基于 Flink 的实时数仓平台建设新进展
LeetCode刷题——715. Range 模块
KunlunDB 查询优化(一)
Analysis on the wallet system architecture of Baidu trading platform
Isolation level of transaction system
Database daily question - day 20: selling products by date
【滑动窗口】leetcode992. Subarrays with K Different Integers
【GO】go多态
Oracle ASM uses the CP command in asmcmd to perform remote replication
How to refine permissions to buttons?
你踩过这些坑吗?谨慎在时间类型列上创建索引