• 1656. Design an Ordered Stream


    package LeetCode_1656
    
    /**
     * 1656. Design an Ordered Stream
     * https://leetcode.com/problems/design-an-ordered-stream/
     * There are n (id, value) pairs, where id is an integer between 1 and n and value is a string.
     * No two pairs have the same id.
    Design a stream that takes the n pairs in an arbitrary order, and returns the values over several calls in increasing order of their ids.
    Implement the OrderedStream class:
    1. OrderedStream(int n) Constructs the stream to take n values and sets a current ptr to 1.
    2. String[] insert(int id, String value) Stores the new (id, value) pair in the stream. After storing the pair:
    a. If the stream has stored a pair with id = ptr, then find the longest contiguous incrementing sequence of ids starting with id = ptr and return a list of the values associated with those ids in order.
    Then, update ptr to the last id + 1.
    b. Otherwise, return an empty list.
    
    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"]]
    Explanation
    OrderedStream os= new OrderedStream(5);
    os.insert(3, "ccccc"); // Inserts (3, "ccccc"), returns [].
    os.insert(1, "aaaaa"); // Inserts (1, "aaaaa"), returns ["aaaaa"].
    os.insert(2, "bbbbb"); // Inserts (2, "bbbbb"), returns ["bbbbb", "ccccc"].
    os.insert(5, "eeeee"); // Inserts (5, "eeeee"), returns [].
    os.insert(4, "ddddd"); // Inserts (4, "ddddd"), returns ["ddddd", "eeeee"].
    
    Constraints:
    1. 1 <= n <= 1000
    2. 1 <= id <= n
    3. value.length == 5
    4. value consists only of lowercase letters.
    5. Each call to insert will have a unique id.
    6. Exactly n calls will be made to insert.
     * */
    class OrderedStream(n: Int) {
    
        var size = 0
        //need global pointer to keep checking
        var curPtr = 0
        var list: ArrayList<String>? = null
    
        init {
            size = n
            list = ArrayList(n)
            for (i in 0 until n) {
                list?.add("")
            }
        }
    
        fun insert(id: Int, value: String): List<String> {
            list!!.set(id - 1, value)
            val result = ArrayList<String>()
            while (curPtr < size && list?.get(curPtr) != "") {
                result.add(list!!.get(curPtr))
                curPtr++
            }
            return result
        }
    }
    /**
     * Your OrderedStream object will be instantiated and called as such:
     * var obj = OrderedStream(n)
     * var param_1 = obj.insert(id,value)
     */
  • 相关阅读:
    Vue 入门. 如何在HTML代码里面快速使用Vue
    CSS3 神器总结
    《JavaScript高级程序设计(第3版)》阅读总结记录第二章之在HTML中使用JavaScript
    《JavaScript高级程序设计(第3版)》阅读总结记录第一章之JavaScript简介
    简单轮播的实现
    怎么作好一个领导者?什么是团队?怎么当好一个组员?
    谈BFC和haslayout
    JavaScript 数组
    ubuntu配置android开发环境和编译源码遇到的一些问题
    C语言文件操作函数大全
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13976346.html
Copyright © 2020-2023  润新知