Problem Description
Design an iterator that alternates (or "zigzags") between two given vectors. For the basic version, given two lists of integers (v1 and v2), the iterator should return elements one by one from each list in turn (i.e., first element of v1, then first element of v2, then second element of v1, etc.). When one list is exhausted, continue iterating over the remaining elements of the other list. Additionally, consider how this can be extended to k vectors following the same cyclic order.
Key Insights
- Use a FIFO (first-in-first-out) structure to maintain the order of vectors which still have remaining elements.
- For each call to next(), select the next element from the current vector and then move that vector to the end of the queue if it still contains elements.
- The approach ensures proper zigzag (or cyclic) ordering among the vectors.
- When extending to k vectors, the same approach works: initialize the queue with each non-empty vector.
Space and Time Complexity
Time Complexity: O(1) per next() call on average, with an overall O(n) for n total elements. Space Complexity: O(k) for the queue structure (where k is the number of vectors), plus the storage for the input vectors.
Solution
We can solve the Zigzag Iterator problem by using a queue (or deque) to hold pairs of the vector’s current index and the vector itself. For each call to next(), we remove the front element from the queue, return the current item from its associated vector, and if the vector has more elements, reinsert the pair (with an updated index) into the back of the queue. This cyclic process naturally interleaves the elements from the provided vectors.