【力扣】1656.设计有序流
【力扣】1656.设计有序流
题目
有 n
个 (id, value)
对,其中 id
是 1
到 n
之间的一个整数,value
是一个字符串。不存在 id
相同的两个 (id, value)
对。
设计一个流,以 任意 顺序获取 n
个 (id, value)
对,并在多次调用时 按 id
递增的顺序 返回一些值。
实现 OrderedStream
类:
-
OrderedStream(int n)
构造一个能接收n
个值的流,并将当前指针ptr
设为1
。 -
String[] insert(int id, String value)
向流中存储新的
(id, value)
对。存储后:- 如果流存储有
id = ptr
的(id, value)
对,则找出从id = ptr
开始的 最长 id 连续递增序列 ,并 按顺序 返回与这些 id 关联的值的列表。然后,将ptr
更新为最后那个id + 1
。
- 如果流存储有
-
否则,返回一个空列表。
示例:
输入
["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
[[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
输出
[null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]
解释
OrderedStream os= new OrderedStream(5);
os.insert(3, "ccccc"); // 插入 (3, "ccccc"),返回 []
os.insert(1, "aaaaa"); // 插入 (1, "aaaaa"),返回 ["aaaaa"]
os.insert(2, "bbbbb"); // 插入 (2, "bbbbb"),返回 ["bbbbb", "ccccc"]
os.insert(5, "eeeee"); // 插入 (5, "eeeee"),返回 []
os.insert(4, "ddddd"); // 插入 (4, "ddddd"),返回 ["ddddd", "eeeee"]
提示:
1 <= n <= 1000
1 <= id <= n
value.length == 5
value
仅由小写字母组成- 每次调用
insert
都会使用一个唯一的id
- 恰好调用
n
次insert
方法一
参考代码
/**
* @param {number} n
*/
var OrderedStream = function (n) {
this.ptr = 1;
this.stream = new Array(n + 1).fill(null);
};
/**
* @param {number} idKey
* @param {string} value
* @return {string[]}
*/
OrderedStream.prototype.insert = function (idKey, value) {
this.stream[idKey] = value;
const result = [];
if (idKey === this.ptr) {
while (this.ptr < this.stream.length && this.stream[this.ptr]) {
result.push(this.stream[this.ptr]);
this.ptr++;
}
}
return result;
};
/**
* Your OrderedStream object will be instantiated and called as such:
* var obj = new OrderedStream(n)
* var param_1 = obj.insert(idKey,value)
*/
思路解析
- 构造函数
constructor
:- 初始化指针
ptr
为1
,表示当前期望的最小id
。 - 创建一个长度为
n + 1
的数组stream
,并用null
填充,该数组用于存储(id, value)
对,id
作为数组的索引。
- 初始化指针
insert
方法:- 首先将新的
(id, value)
对存储到stream
数组中,即this.stream[id] = value
。 - 然后判断插入的
id
是否等于当前指针ptr
,若相等,则表示找到了一个可输出的连续序列。 - 使用
while
循环,只要当前ptr
对应的数组位置有值,就将该值推入结果数组result
中,并将ptr
向后移动一位。 - 最后返回结果数组
result
,若id
不等于ptr
,则返回空数组。
- 首先将新的
方法二(摘自灵茶山艾府)
参考代码
var OrderedStream = function(n) {
this.a = Array(n + 1);
this.ptr = 1;
};
OrderedStream.prototype.insert = function(idKey, value) {
this.a[idKey] = value;
let start = this.ptr;
while (this.ptr < this.a.length && this.a[this.ptr] !== undefined) {
this.ptr++;
}
return this.a.slice(start, this.ptr);
};
思路解析
- 存储新的
(id, value)
对:this.a[idKey] = value;
:将新的(idKey, value)
对存储到数组this.a
中,idKey
作为数组的索引。
- 记录起始位置:
let start = this.ptr;
:记录当前指针ptr
的位置,作为后续返回连续value
数组的起始索引。
- 查找连续的
value
序列:while (this.ptr < this.a.length && this.a[this.ptr] !== undefined) { this.ptr++; }
:使用while
循环从当前指针ptr
开始检查数组元素。只要ptr
在数组长度范围内且对应的数组元素不为undefined
(即该位置已经存储了value
),就将ptr
向后移动一位。这样可以找到从ptr
开始的最长连续id
序列的结束位置。
- 返回连续的
value
数组:return this.a.slice(start, this.ptr);
:使用slice
方法从数组this.a
中截取从start
到ptr
之间的元素,返回这个连续的value
数组。如果idKey
不等于ptr
,则start
和ptr
相等,返回的数组为空。
复杂度分析
- 时间复杂度:初始化 O(n),insert 均摊 O(1),因为 insert 恰好调用 n 次,所以 ptr 一共移动恰好 n 次,平均每次调用移动 1 次。
tr,则
start和
ptr` 相等,返回的数组为空。
复杂度分析
- 时间复杂度:初始化 O(n),insert 均摊 O(1),因为 insert 恰好调用 n 次,所以 ptr 一共移动恰好 n 次,平均每次调用移动 1 次。
- 空间复杂度:初始化 O(n),insert 是 O(n) 或者 O(1),取决于语言。