Backtracking pattern

N-Queens

Given an integer n, return all distinct ways to place n queens on an n-by-n chessboard so that no two queens attack each other, with each solution shown as a board layout. Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • LeetCode 51
  • Hard
  • 1 approach

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

BacktrackingLeetCode 51

Hard

The approach

  1. 01 Backtracking · one queen per row O(N!)O(N) space
Played step by step, with its time and space.

What you will learn

  • How to read N-Queens and spot the backtracking pattern in it
  • Backtracking · one queen per row: O(N!) time, O(N) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Backtracking

See N-Queens run, step by step

Sign in and this page opens on the animation, with all 8 Backtracking problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.