当前位置:网站首页>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 receiven
A stream of values , And put the current pointerptr
Set to1
.String[] insert(int id, String value)
Store new
(id, value)
Yes . After storage :
- If the stream store has
id = ptr
Of(id, value)
Yes , Find out fromid = ptr
At the beginning The longest id A sequence of successive increments , and According to the order Back with these id List of associated values . then , takeptr
Update 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 <= 1000
1 <= id <= n
value.length == 5
value
It's only made up of lowercase letters- Every time you call
insert
Will use a uniqueid
- Just call
n
Timeinsert
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).
边栏推荐
- Daily code 300 lines learning notes day 10
- 2022.02.14_ Daily question leetcode five hundred and forty
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Basis of target detection (IOU)
- 【剑指 Offer 】64. 求1+2+…+n
- Simulink oscilloscope data is imported into Matlab and drawn
- Threejs Part 2: vertex concept, geometry structure
- Aike AI frontier promotion (7.3)
- Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
- Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
猜你喜欢
QT serial port UI design and solution to display Chinese garbled code
Cocos Creator 2.x 自动打包(构建 + 编译)
NSQ source code installation and operation process
Mysql database -dql
CC2530 common registers for serial communication
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
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
2022.02.14_ Daily question leetcode five hundred and forty
Cocos Creator 2. X automatic packaging (build + compile)
MySQL converts comma separated attribute field data from column to row
随机推荐
TCP congestion control details | 3 design space
word 退格键删除不了选中文本,只能按delete
[statement] about searching sogk1997 and finding many web crawler results
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
Aike AI frontier promotion (7.3)
Netease UI automation test exploration: airtest+poco
NSQ源码安装运行过程
CC2530 common registers for crystal oscillator settings
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
爱可可AI前沿推介(7.3)
[combinatorics] non descending path problem (number of non descending paths with constraints)
Difference between JSON and bson
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
Capacités nécessaires à l'analyse des données
A survey of state of the art on visual slam
ThreeJS 第二篇:顶点概念、几何体结构
浅谈拉格朗日插值及其应用
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
Summary of three methods of PHP looping through arrays list (), each (), and while