package leetcode;

import java.util.List;

/**
 * https://leetcode.com/problems/design-an-ordered-stream/
 */
public class Problem1656 {
    static class OrderedStream {
        public OrderedStream(int n) {
            // TODO
        }

        public List<String> insert(int id, String value) {
            // TODO
            return null;
        }
    }


    public static void main(String[] args) {
        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"].
    }
}

@@ -12,16 +12,44 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -204,56 +204,164 @@
    p
-ublic OrderedStream(int n) %7B%0A            // TODO
+rivate final String%5B%5D strings;%0A        private int ptr;%0A%0A        public OrderedStream(int n) %7B%0A            strings = new String%5Bn + 1%5D;%0A            ptr = 1;
 %0A   
@@ -443,39 +443,78 @@
     
-// TODO%0A            return null
+List%3CString%3E list = new ArrayList%3C%3E();%0A            strings%5Bid%5D = value
 ;%0A  
@@ -523,489 +523,285 @@
     
-%7D%0A
     
-%7D%0A%0A%0A    public static void main(String%5B%5D args) %7B%0A        OrderedStream os= new OrderedStream(5);%0A        os.insert(3, %22ccccc%22); // Inserts (3, %22ccccc%22), returns %5B%5D.%0A        os.insert(1, %22aaaaa%22); // Inserts (1, %22aaaaa%22), returns %5B%22aaaaa%22%5D.%0A        os.insert(2, %22bbbbb%22); // Inserts (2, %22bbbbb%22), returns %5B%22bbbbb%22, %22ccccc%22%5D.%0A        os.insert(5, %22eeeee%22); // Inserts (5, %22eeeee%22), returns %5B%5D.%0A        os.insert(4, %22ddddd%22); // Inserts (4, %22ddddd%22), returns %5B%22ddddd%22, %22eeeee%22%5D.
+if (ptr == id && strings%5Bid%5D != null) %7B%0A                int i = id;%0A                for (; i %3C strings.length && strings%5Bi%5D != null; i++) %7B%0A                    list.add(strings%5Bi%5D);%0A                %7D%0A                ptr = i;%0A            %7D%0A            return list;%0A        %7D
 %0A   

Solve problem 1656