Friday, February 19, 2021

Leetcode Everyday: 1656. Design an Ordered Stream. Easy

public class OrderedStream {
    
    int _ptr = 1;
    readonly int _max;
    readonly string[] _list;

    public OrderedStream(int n) {
        _list = new string[n + 1];
        _max = n;
    }
    
    public IList<string> Insert(int idKey, string value) {       
        _list[idKey] = value;
        
        int start = _ptr;
        while (_ptr <= _max && _list[_ptr] != null) {
            ++_ptr;
        }
        
      
        // 424 ms.  
        // return _list[start.._ptr];        
        
        // 376 ms. For some reasons, this is faster
        return new Span<string>(_list, start, length: _ptr - start).ToArray();
    }
}
Fastest

Source: https://leetcode.com/problems/design-an-ordered-stream/

No comments:

Post a Comment