Problem overview
The method centerOfGrid receives a 3×3 grid of integers and returns the number in the middle row and the middle column. A grid is a table of numbers made of rows: this one has 3 rows with 3 numbers each. Rows and columns are counted from 0, so the center is row 1, column 1. For [[1, 2, 3], [4, 5, 6], [7, 8, 9]] the answer is 5. For [[9, 8, 7], [6, -5, 4], [3, 2, 1]] it is -5: the center can be any integer, a negative one too.
Approach
In TypeScript a grid is an array whose items are arrays. Its type is number[][]: an array of number[], where each number[] is one row. Reading it takes two steps, and each one uses square brackets:
grid[row]is a whole row, for example[4, 5, 6]for row 1 of the first example;grid[row][col]is the number in that row at that column.
Indexes start at 0, so a row with 3 numbers has the columns 0, 1 and 2, and the middle one is 1. The same holds for the 3 rows. The center is grid[1][1].
Step by step
- Pick the middle row.
grid[1]is the array in the middle. - Pick the middle column of that row. Adding a second index,
grid[1][1], reads the number you want. - Return it. The whole function is one
returnstatement.
The solution in TypeScript
class Solution {
centerOfGrid(grid: number[][]): number {
return grid[1][1];
}
}
Both indexes are evaluated from left to right. First grid[1] gives a row, and then [1] is read from that row. For the second example, grid[1] is [6, -5, 4] and grid[1][1] is -5.
Rows are arrays too
Each row is an ordinary array, so everything you know about arrays applies to it. The name grid[1] refers to the actual middle row, not to a copy. If you store it in a variable, let row = grid[1], and then assign row[1] = 50, the grid itself changes to [[1, 2, 3], [4, 50, 6], [7, 8, 9]]. This lesson only reads, so nothing is modified, but the fact explains why a grid is called an array of arrays and why the double index works: the first index finds the row, and the second index works on that row as on any array.
Reading other cells
The same notation reaches any cell. For [[1, 2, 3], [4, 5, 6], [7, 8, 9]]:
| Expression | Value |
|---|---|
grid[0][2] | 3 |
grid[1][1] | 5 |
grid[2][0] | 7 |
grid.length | 3 |
grid[0].length | 3 |
The first index is always the row and the second the column. Writing them in the opposite order mixes the two up, which gives a different cell for any grid that isn't symmetric.
Edge cases and common mistakes
- Counting from 1.
grid[2][2]is the bottom-right corner, not the center. Indexes start at 0. - Using one index.
grid[1]is the entire middle row, an array, not a number. - Going outside the grid. With only 3 rows,
grid[3]isundefined, and readinggrid[3][0]stops the program withTypeError: Cannot read properties of undefined (reading '0'). The lesson's grids always have 3 rows of 3 numbers, so index 1 always exists. - Other grid sizes. This solution is written for 3×3. For a grid whose side is odd, the middle index would be the side divided by 2, rounded down.
- Negative values. A center such as
-5needs no special handling. The function returns whatever is stored there.
Testing
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]gives5.[[0, 0, 0], [0, 7, 0], [0, 0, 0]]gives7.[[9, 8, 7], [6, -5, 4], [3, 2, 1]]gives-5.
