Zigzag Grid Traversal With Skip - Problem

Imagine you're navigating through a 2D grid following a specific zigzag pattern, but with a twist - you must skip every alternate cell during your journey!

Given an m x n 2D array grid of positive integers, your task is to traverse the grid in a zigzag pattern while collecting values from only every other cell you encounter.

Zigzag Traversal Rules:

  • πŸš€ Start at the top-left corner (0, 0)
  • ➑️ Move right across the first row until you reach the end
  • ⬇️ Drop down to the next row
  • ⬅️ Move left across this row until you reach the beginning
  • πŸ”„ Continue alternating between right and left traversal for each row
  • ⏭️ Skip every alternate cell during the entire traversal

Return an array containing the values of the cells you visit (not skip) in the order you encounter them.

Input & Output

example_1.py β€” Basic 3x3 Grid
$ Input: grid = [[1,2,3],[6,5,4],[7,8,9]]
β€Ί Output: [1,3,5]
πŸ’‘ Note: Zigzag order: 1β†’2β†’3β†’6β†’5β†’4β†’7β†’8β†’9. With skipping alternates: positions 0,2,4 give us [1,3,5]
example_2.py β€” 2x4 Rectangle Grid
$ Input: grid = [[1,2,3,4],[8,7,6,5]]
β€Ί Output: [1,3,8,6]
πŸ’‘ Note: Zigzag order: 1β†’2β†’3β†’4β†’5β†’6β†’7β†’8. Taking positions 0,2,4,6 gives us [1,3,5,7]. Wait, let me recalculate: 1β†’2β†’3β†’4β†’8β†’7β†’6β†’5, so [1,3,8,6]
example_3.py β€” Single Row Edge Case
$ Input: grid = [[1,2,3,4,5]]
β€Ί Output: [1,3,5]
πŸ’‘ Note: Single row traversal left-to-right: 1β†’2β†’3β†’4β†’5. Taking alternate positions 0,2,4 gives us [1,3,5]

Visualization

Tap to expand
Grid (Book Pages)124356Pos: 0,1,2,3,4,5Position Counter LogicStep-by-step Collection:β€’ Position 0: Collect value 1 βœ“β€’ Position 1: Skip value 2 βœ—β€’ Position 2: Collect value 3 βœ“β€’ Position 3: Skip value 4 βœ—β€’ Position 4: Collect value 5 βœ“β€’ Position 5: Skip value 6 βœ—Result: [1, 3, 5]Final Answer135...
Understanding the Visualization
1
Start Top-Left
Begin at position (0,0) with counter = 0
2
Read First Line Left→Right
Move right across first row, collecting at even positions
3
Drop to Next Line
Move down to second row, continue position counter
4
Read Second Line Right→Left
Move left across second row, still collecting at even positions
5
Continue Pattern
Alternate row directions until complete
Key Takeaway
🎯 Key Insight: Use a single position counter across the entire zigzag traversal to determine which cells to collect, making it a clean O(mΓ—n) single-pass solution

Time & Space Complexity

Time Complexity
⏱️
O(m Γ— n)

Visit each cell exactly once in the grid

n
2n
βœ“ Linear Growth
Space Complexity
O(k)

Where k is the number of collected elements (roughly half of total elements)

n
2n
βœ“ Linear Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≀ m, n ≀ 104
  • 1 ≀ grid[i][j] ≀ 105
  • The grid will always have at least one element
Asked in
Google 23 Amazon 18 Microsoft 15 Meta 12
41.3K Views
Medium Frequency
~12 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen