251. Flatten 2D Vector
Flatten 2D Vector (LeetCode 251) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
How to solve Flatten 2D Vector
Flatten 2D Vector is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.
Work towards the solution in these steps:
- How many variables do you need to keep track?
- Two variables is all you need. Try with <code>x</code> and <code>y</code>.
- Beware of empty rows. It could be the first few rows.
The reference solution runs in O(N) time and O(N) space.
The Flatten 2D Vector solution and editorial walks through the full approach with reference code.
Problem statement
You are given a 2D vector vec, represented as a list of lists of integers. Design an iterator that flattens the 2D vector and iterates through all the elements in row-major order (i.e., from left to right and top to bottom).
Implement the Vector2D class:
- Vector2D(int[][] vec) initializes the object with the 2D vector vec. - int next() returns the next element in the flattened vector. - boolean hasNext() returns true if there are still elements to be returned, or false otherwise.
Your implementation should support calling next() and hasNext() multiple times and in any order.
Return the appropriate values for each operation as described above.
Examples
Example 1
Input: {"vec2d": [[1, 2], [3], [4, 5, 6]]}
Output: [1, 2, 3, 4, 5, 6]
Explanation: Flattens a regular 2D vector with multiple rows of varying lengths.
Example 2
Input: {"vec2d": [[1], [2], [3]]}
Output: [1, 2, 3]
Explanation: Flattens a 2D vector where each sub-array contains a single element.
Topics and companies
Topics: Array, Two Pointers, Design, Iterator.
Reported in interviews at Airbnb.
How to practise Flatten 2D Vector on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(N) space.
Open Flatten 2D Vector in the code editor, or read the Flatten 2D Vector editorial for a worked solution with its approach and complexity analysis.
Where Flatten 2D Vector sits in the DSA pattern sheet
Problems related to Flatten 2D Vector
Other problems that use the same Design (General/Specific) and Design Patterns techniques:
- All O`one Data Structure — Hard, same subpattern
- Design a Stack With Increment Operation — Medium, same subpattern
- Design a Text Editor — Hard, same subpattern
- Design Circular Deque — Medium, same subpattern
- Design Circular Queue — Medium, same subpattern
- Design Compressed String Iterator — Easy, same subpattern
Flatten 2D Vector FAQ
What is the LeetCode number of Flatten 2D Vector?
Flatten 2D Vector is LeetCode problem 251, rated Medium.
What pattern is used to solve Flatten 2D Vector?
Flatten 2D Vector is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
What is the time complexity of Flatten 2D Vector?
The reference solution runs in O(N) time and O(N) space.
Which companies ask Flatten 2D Vector?
Flatten 2D Vector has been reported in interviews at Airbnb.