You are given an m x n 2D array grid of positive integers.
Your task is to traverse grid in a zigzag pattern while skipping every alternate cell.
Zigzag pattern traversal is defined as following the below actions:
(0, 0).Note that you must skip every alternate cell during the traversal.
Return an array of integers result containing, in order, the value of the cells visited during the zigzag traversal with skips.
Example 1:
Input: grid = [[1,2],[3,4]]
Output: [1,4]
Explanation:

Example 2:
Input: grid = [[2,1],[2,1],[2,1]]
Output: [2,1,2]
Explanation:

Example 3:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,3,5,7,9]
Explanation:

Constraints:
2 <= n == grid.length <= 502 <= m == grid[i].length <= 501 <= grid[i][j] <= 2500When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To solve this problem using the brute force approach, we simply follow the exact zigzag path through the grid from start to finish. We visit every single cell one by one while keeping track of our journey by collecting every other item we encounter.
Here's how the algorithm would work step-by-step:
def zigzag_grid_traversal_with_skip(grid):
collected_items = []
# Flatten the grid into a single alternating path by reversing every other row
for row_index in range(len(grid)):
current_row = grid[row_index]
if row_index % 2 != 0:
current_row = current_row[::-1]
for item in current_row:
collected_items.append(item)
result_items = []
# Filter the path by skipping every second item starting from the very first one
for item_index in range(len(collected_items)):
if item_index % 2 == 0:
result_items.append(collected_items[item_index])
return result_itemsThis solution traverses a 2D grid in a zigzag pattern from top to bottom. Even rows are traversed from left to right, while odd rows are traversed from right to left. During the entire continuous traversal, every alternate cell is collected starting with the initial top-left cell.
Here's how the algorithm would work step-by-step:
def zigzag_traversal(grid):
result = []
include_current = True
# Iterate through each row index in the grid
for row_index in range(len(grid)):
current_row = grid[row_index]
# Reverse row elements for odd-indexed rows to achieve zigzag order
row_values = current_row if row_index % 2 == 0 else current_row[::-1]
# Traverse the ordered row values and pick every alternate element
for value in row_values:
if include_current:
result.append(value)
include_current = not include_current
return result
| Case | How to Handle |
|---|---|
| Empty grid or null input where rows or columns equal zero | Check for null or empty dimensions at the start and return an empty array immediately. |
| Single cell grid with dimensions one by one | The traversal loop executes once, correctly capturing the single element without any out of bounds errors. |
| Grid with a single row and multiple columns | The row index remains zero, meaning it only traverses left to right while correctly applying the skip logic. |
| Grid with a single column and multiple rows | Alternating directions still evaluate correctly per row, visiting every alternate row and cell as required. |
| Large grid dimensions pushing memory limits near maximum constraints | The algorithm processes the grid iteratively with a single output list, maintaining optimal linear space and time complexity. |
| Grid containing negative numbers, zeros, and extreme boundary integer values | Since the task is traversal rather than arithmetic aggregation, numerical values are simply appended directly without risk of overflow. |
| Grid with alternating row lengths or ragged arrays | The loop bounds check each specific row length before accessing elements to prevent index out of bounds exceptions. |
| Grid where the skip pattern causes all elements in odd rows to be bypassed | The step increment of two handles the skip pattern uniformly across both directions without skipping entire rows unintentionally. |