当前位置: 首页 > article >正文

【力扣】1656.设计有序流

【力扣】1656.设计有序流

题目

n(id, value) 对,其中 id1n 之间的一个整数,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
  • 否则,返回一个空列表。

示例:

img

输入
["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
  • 恰好调用 ninsert

方法一

参考代码

/**
 * @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)
 */

思路解析

  1. 构造函数 constructor:
    • 初始化指针 ptr1,表示当前期望的最小 id
    • 创建一个长度为 n + 1 的数组 stream,并用 null 填充,该数组用于存储 (id, value) 对,id 作为数组的索引。
  2. 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);
};

思路解析

  1. 存储新的 (id, value)
    • this.a[idKey] = value;:将新的 (idKey, value) 对存储到数组 this.a 中,idKey 作为数组的索引。
  2. 记录起始位置
    • let start = this.ptr;:记录当前指针 ptr 的位置,作为后续返回连续 value 数组的起始索引。
  3. 查找连续的 value 序列
    • while (this.ptr < this.a.length && this.a[this.ptr] !== undefined) { this.ptr++; }:使用 while 循环从当前指针 ptr 开始检查数组元素。只要 ptr 在数组长度范围内且对应的数组元素不为 undefined(即该位置已经存储了 value),就将 ptr 向后移动一位。这样可以找到从 ptr 开始的最长连续 id 序列的结束位置。
  4. 返回连续的 value 数组
    • return this.a.slice(start, this.ptr);:使用 slice 方法从数组 this.a 中截取从 startptr 之间的元素,返回这个连续的 value 数组。如果 idKey 不等于 ptr,则 startptr 相等,返回的数组为空。

复杂度分析

  • 时间复杂度:初始化 O(n),insert 均摊 O(1),因为 insert 恰好调用 n 次,所以 ptr 一共移动恰好 n 次,平均每次调用移动 1 次。
    tr,则 startptr` 相等,返回的数组为空。

复杂度分析

  • 时间复杂度:初始化 O(n),insert 均摊 O(1),因为 insert 恰好调用 n 次,所以 ptr 一共移动恰好 n 次,平均每次调用移动 1 次。
  • 空间复杂度:初始化 O(n),insert 是 O(n) 或者 O(1),取决于语言。

http://www.kler.cn/a/562618.html

相关文章:

  • 计算机网络:ICMP协议(Internet控制消息协议)介绍
  • Vue.js 与 Ajax(Axios)的深入探索
  • 关于网关和ip地址怎么理解?
  • CSS滤镜(filter)和混合模式(blend mode)的使用场景
  • 如何禁用uniapp,vue页面下拉刷新功能
  • 禁止别人调试前端代码
  • 基于Matlab实现汽车远近光灯识别的详细步骤及代码示例
  • C#中级教程(1)——解锁 C# 编程的调试与错误处理秘籍
  • 一文讲解Redis中的数据一致性问题
  • 爬虫开源项目
  • 探索浮点数在内存中的存储(附带快速计算补码转十进制)
  • 电子科技大学考研复习经验分享
  • 1.1部署es:9200
  • 第九节: Vue 3 中的 provide 与 inject:优雅的跨组件通信
  • SpringSecurity核心过滤器-SecurityContextPersistenceFilter
  • uniapp写的h5跳转小程序
  • LabVIEW 中 codeGenEngine.llb 工具库
  • 【c语言】字符函数和字符串函数(1)
  • 【SpringBoot】——分组校验、自定义注解、登入验证(集成redis)、属性配置方式、多环境开发系统学习知识
  • llaMa模型的创新