当前位置:网站首页>LeetCode 1656. Design ordered flow
LeetCode 1656. Design ordered flow
2022-07-03 16:44:00 【Daylight629】
1656. Design ordered flow
Yes n individual (id, value) Yes , among id yes 1 To n An integer between ,value Is a string . non-existent id The same two (id, value) Yes .
Design a stream , With arbitrarily Sequential acquisition n individual (id, value) Yes , And when called many times Press id Increasing order Return some values .
Realization OrderedStream class :
OrderedStream(int n)Construct one that can receivenA stream of values , And put the current pointerptrSet to1.String[] insert(int id, String value)Store new
(id, value)Yes . After storage :
- If the stream store has
id = ptrOf(id, value)Yes , Find out fromid = ptrAt the beginning The longest id A sequence of successive increments , and According to the order Back with these id List of associated values . then , takeptrUpdate to the last oneid + 1. - otherwise , Return an empty list .
- If the stream store has
Example :

Input
["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
[[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
Output
[null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]
explain
OrderedStream os= new OrderedStream(5);
os.insert(3, "ccccc"); // Insert (3, "ccccc"), return []
os.insert(1, "aaaaa"); // Insert (1, "aaaaa"), return ["aaaaa"]
os.insert(2, "bbbbb"); // Insert (2, "bbbbb"), return ["bbbbb", "ccccc"]
os.insert(5, "eeeee"); // Insert (5, "eeeee"), return []
os.insert(4, "ddddd"); // Insert (4, "ddddd"), return ["ddddd", "eeeee"]
Tips :
1 <= n <= 10001 <= id <= nvalue.length == 5valueIt's only made up of lowercase letters- Every time you call
insertWill use a uniqueid - Just call
nTimeinsert
Two 、 Method 1
class OrderedStream {
private int ptr = 1;
private final String[] strs;
public OrderedStream(int n) {
strs = new String[n + 1];
}
public List<String> insert(int idKey, String value) {
strs[idKey] = value;
List<String> res = new ArrayList<>();
while (ptr < strs.length && strs[ptr] != null) {
res.add(strs[ptr]);
ptr++;
}
return res;
}
}
/** * Your OrderedStream object will be instantiated and called as such: * OrderedStream obj = new OrderedStream(n); * List<String> param_1 = obj.insert(idKey,value); */
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(n).
边栏推荐
- Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
- There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
- NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri
- ThreeJS 第二篇:顶点概念、几何体结构
- PHP production website active push (website)
- Interviewer: how does the JVM allocate and recycle off heap memory
- IL Runtime
- Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
- 浅谈拉格朗日插值及其应用
- Thread pool executes scheduled tasks
猜你喜欢

2022爱分析· 国央企数字化厂商全景报告

Data driving of appium framework for mobile terminal automated testing

What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties

Basis of target detection (IOU)

There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
![[combinatorics] non descending path problem (number of non descending paths with constraints)](/img/89/bd1a2ddd9632ab5d4b4bee9336be51.jpg)
[combinatorics] non descending path problem (number of non descending paths with constraints)

NSQ源码安装运行过程

What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR

CC2530 common registers for serial communication

斑馬識別成狗,AI犯錯的原因被斯坦福找到了
随机推荐
SVN使用规范
PHP中register_globals参数设置
斑马识别成狗,AI犯错的原因被斯坦福找到了
Develop team OKR in the way of "crowdfunding"
Caching mechanism of Hibernate / session level caching mechanism
PHP二级域名session共享方案
Idea configuration plug-in
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
The word backspace key cannot delete the selected text, so you can only press Delete
NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri
数据分析必备的能力
CC2530 common registers for crystal oscillator settings
數據分析必備的能力
Eleven requirements for test management post
14 topics for performance interviews between superiors and subordinates (4)
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
跟我学企业级flutter项目:简化框架demo参考
AcWing 第58 场周赛
Mysql 将逗号隔开的属性字段数据由列转行