Zigzag Grid Traversal With Skip

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 these 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 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
1 2
3 4
Inputgrid = [[1,2],[3,4]]
Output[1,4]
The zigzag order is [1, 2, 4, 3], and keeping every other cell gives [1, 4].
Example 2
2 1
2 1
2 1
Inputgrid = [[2,1],[2,1],[2,1]]
Output[2,1,2]
The zigzag order is [2, 1, 1, 2, 2, 1], and keeping every other cell gives [2, 1, 2].

Constraints

  • 2 <= n == grid.length <= 50
  • 2 <= m == grid[i].length <= 50
  • 1 <= grid[i][j] <= 2500

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate