设计有序流

设计有序流

  1. Design an Ordered Stream
    There is a stream of n (idKey, value) pairs arriving in an arbitrary order, where idKey is an integer between 1 and n and value is a string. No two pairs have the same id.
    Design a stream that returns the values in increasing order of their IDs by returning a chunk (list) of values after each insertion. The concatenation of all the chunks should result in a list of the sorted values.
    Implement the OrderedStream class:
    OrderedStream(int n) Constructs the stream to take n values.
    String[] insert(int idKey, String value) Inserts the pair (idKey, value) into the stream, then returns the largest possible chunk of currently inserted values that appear next in the order.
	class OrderedStream {
		int ptr;
		Map<Integer,String> map ;
	    public OrderedStream(int n) {
	    	map = new HashMap<Integer,String>(n/3*4+1);
	    	ptr = 1;
	    }
	    
	    public List<String> insert(int idKey, String value) {
	    	map.put(idKey, value);
	    	List<String> list = new ArrayList<String>();
	    	while(map.containsKey(ptr)) {
	    		list.add(map.get(ptr++));
	    	}
	    	return list;
	    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值