Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a non-negative integer n representing a 2n x 2n grid. You must fill the grid with integers from 0 to 22n - 1 to make it special. A grid is special if it satisfies all the following conditions:
Return the special 2n x 2n grid.
Note: Any 1x1 grid is special.
Example 1:
Input: n = 0
Output: [[0]]
Explanation:
The only number that can be placed is 0, and there is only one possible position in the grid.
Example 2:
Input: n = 1
Output: [[3,0],[2,1]]
Explanation:
The numbers in each quadrant are:
Since 0 < 1 < 2 < 3, this satisfies the given constraints.
Example 3:
Input: n = 2
Output: [[15,12,3,0],[14,13,2,1],[11,8,7,4],[10,9,6,5]]
Explanation:
The numbers in each quadrant are:
max(3, 0, 2, 1) < min(7, 4, 6, 5)max(7, 4, 6, 5) < min(11, 8, 10, 9)max(11, 8, 10, 9) < min(15, 12, 14, 13)This satisfies the first three requirements. Additionally, each quadrant is also a special grid. Thus, this is a special grid.
Constraints:
0 <= n <= 10Problem summary: You are given a non-negative integer n representing a 2n x 2n grid. You must fill the grid with integers from 0 to 22n - 1 to make it special. A grid is special if it satisfies all the following conditions: All numbers in the top-right quadrant are smaller than those in the bottom-right quadrant. All numbers in the bottom-right quadrant are smaller than those in the bottom-left quadrant. All numbers in the bottom-left quadrant are smaller than those in the top-left quadrant. Each of its quadrants is also a special grid. Return the special 2n x 2n grid. Note: Any 1x1 grid is special.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
0
1
2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3537: Fill a Special Grid
class Solution {
public int[][] specialGrid(int n) {
final int sz = 1 << n;
int[][] grid = new int[sz][sz];
fill(grid, 0, sz, 0, sz);
return grid;
}
private int count = 0;
private void fill(int[][] grid, int x1, int x2, int y1, int y2) {
if (x2 - x1 == 1) {
grid[x1][y1] = count++;
return;
}
int midRow = (x1 + x2) / 2;
int midCol = (y1 + y2) / 2;
fill(grid, x1, midRow, midCol, y2);
fill(grid, midRow, x2, midCol, y2);
fill(grid, midRow, x2, y1, midCol);
fill(grid, x1, midRow, y1, midCol);
}
}
// Accepted solution for LeetCode #3537: Fill a Special Grid
package main
// https://space.bilibili.com/206214
func specialGrid(n int) [][]int {
val := 0
var dfs func([][]int, int)
dfs = func(a [][]int, l int) {
if len(a) == 1 {
a[0][l] = val
val++
return
}
m := len(a) / 2
dfs(a[:m], l+m)
dfs(a[m:], l+m)
dfs(a[m:], l)
dfs(a[:m], l)
}
a := make([][]int, 1<<n)
for i := range a {
a[i] = make([]int, 1<<n)
}
dfs(a, 0)
return a
}
func specialGrid2(n int) [][]int {
a := make([][]int, 1<<n)
for i := range a {
a[i] = make([]int, 1<<n)
}
val := 0
var dfs func(int, int, int, int)
dfs = func(u, d, l, r int) {
if d-u == 1 {
a[u][l] = val
val++
return
}
m := (d - u) / 2
dfs(u, u+m, l+m, r)
dfs(u+m, d, l+m, r)
dfs(u+m, d, l, l+m)
dfs(u, u+m, l, l+m)
}
dfs(0, 1<<n, 0, 1<<n)
return a
}
# Accepted solution for LeetCode #3537: Fill a Special Grid
class Solution:
def specialGrid(self, n: int) -> list[list[int]]:
sz = 1 << n
grid = [[0] * sz for _ in range(sz)]
count = 0
def fill(x1: int, x2: int, y1: int, y2: int) -> None:
nonlocal count
if x2 - x1 == 1:
grid[x1][y1] = count
count += 1
return
midRow = (x1 + x2) // 2
midCol = (y1 + y2) // 2
fill(x1, midRow, midCol, y2)
fill(midRow, x2, midCol, y2)
fill(midRow, x2, y1, midCol)
fill(x1, midRow, y1, midCol)
fill(0, sz, 0, sz)
return grid
// Accepted solution for LeetCode #3537: Fill a Special Grid
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3537: Fill a Special Grid
// class Solution {
// public int[][] specialGrid(int n) {
// final int sz = 1 << n;
// int[][] grid = new int[sz][sz];
// fill(grid, 0, sz, 0, sz);
// return grid;
// }
//
// private int count = 0;
//
// private void fill(int[][] grid, int x1, int x2, int y1, int y2) {
// if (x2 - x1 == 1) {
// grid[x1][y1] = count++;
// return;
// }
// int midRow = (x1 + x2) / 2;
// int midCol = (y1 + y2) / 2;
// fill(grid, x1, midRow, midCol, y2);
// fill(grid, midRow, x2, midCol, y2);
// fill(grid, midRow, x2, y1, midCol);
// fill(grid, x1, midRow, y1, midCol);
// }
// }
// Accepted solution for LeetCode #3537: Fill a Special Grid
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3537: Fill a Special Grid
// class Solution {
// public int[][] specialGrid(int n) {
// final int sz = 1 << n;
// int[][] grid = new int[sz][sz];
// fill(grid, 0, sz, 0, sz);
// return grid;
// }
//
// private int count = 0;
//
// private void fill(int[][] grid, int x1, int x2, int y1, int y2) {
// if (x2 - x1 == 1) {
// grid[x1][y1] = count++;
// return;
// }
// int midRow = (x1 + x2) / 2;
// int midCol = (y1 + y2) / 2;
// fill(grid, x1, midRow, midCol, y2);
// fill(grid, midRow, x2, midCol, y2);
// fill(grid, midRow, x2, y1, midCol);
// fill(grid, x1, midRow, y1, midCol);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.