Decode Algorithms & LeetCode
  • Decode Algorithms & LeetCode
  • G家练习
  • Array
    • Two Sum
    • Two Pointers
    • PrefixSum
    • Matrix
    • Interval & Sweepline
    • Sort
    • Iterator
    • Segment Tree
  • Binary Search
    • 教学
    • Binary Search on Matrix
    • Binary Search To Find An Answer
  • String
    • Parenthesis
    • Sliding Window
    • Trie
    • String arrangement
  • LinkedList
  • Tree
    • Level Order Traversal
    • BST or Inorder
    • Construst Tree
    • Prefix Tree
  • Stack
  • Heap
  • Greedy
  • BFS
  • DFS
    • DFS on String
    • Backtracking
    • DFS+Memo
  • Graph
    • Union Find
    • Topological Sort
    • Dijkstra
    • Bipartite Graph
    • Minimum Spanning Trees (MST)
    • Traveling Salesman Problem (TSP)
  • Dynamic Programing
    • DP on String
    • Knapsack Problem
  • Design
    • Concurrency
  • Math
  • Bit Manipulation
  • Divide & Conquer
Powered by GitBook
On this page

Was this helpful?

Divide & Conquer

PreviousBit Manipulation

Last updated 13 days ago

Was this helpful?

function specialGrid(n: number): number[][] {
  const size = 1 << n;
  const grid: number[][] = Array.from({ length: size }, () =>
    new Array(size).fill(0)
  );

  let val = 0;

  function solve(r: number, c: number, size: number): void {
    if (size === 1) {
      grid[r][c] = val++;
      return;
    }
    const s = size / 2;
    solve(r, c + s, s); // top right
    solve(r + s, c + s, s); // bottom right
    solve(r + s, c, s); // bottom left
    solve(r, c, s); // top left
  }

  solve(0, 0, size);

  return grid;
}
3537. Fill a Special Grid