Zigzag Grid Traversal With Skip - Problem
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:
- Start at the top-left cell
(0, 0) - Move right within a row until the end of the row is reached
- Drop down to the next row, then traverse left until the beginning of the row is reached
- Continue alternating between right and left traversal until every row has been traversed
Note: 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.
Input & Output
Example 1 — Basic 2x3 Grid
$
Input:
grid = [[1,2,3],[4,5,6]]
›
Output:
[1,3,5]
💡 Note:
Zigzag traversal: 1→2→3, then 6→5→4. With skipping: take positions 0,2,4 which are values 1,3,5
Example 2 — Single Row
$
Input:
grid = [[1,2,3,4]]
›
Output:
[1,3]
💡 Note:
Only one row, traverse left to right: 1→2→3→4. Skip alternates: take positions 0,2 which are 1,3
Example 3 — Single Column
$
Input:
grid = [[1],[2],[3]]
›
Output:
[1,3]
💡 Note:
Zigzag on single column: 1 (pos 0), 2 (pos 1), 3 (pos 2). Take even positions: 1,3
Constraints
- 1 ≤ m, n ≤ 100
- 1 ≤ grid[i][j] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input Grid
2D grid with positive integers
2
Zigzag + Skip
Traverse in zigzag pattern, skip every alternate cell
3
Result Array
Values from non-skipped cells in traversal order
Key Takeaway
🎯 Key Insight: Use position counter during zigzag traversal to directly identify cells to include
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code