Skip to content

Implement N Queens Solution Dart#538

Open
rrbharath wants to merge 2 commits intocodedecks-in:masterfrom
rrbharath:n-queens-dart-solution
Open

Implement N Queens Solution Dart#538
rrbharath wants to merge 2 commits intocodedecks-in:masterfrom
rrbharath:n-queens-dart-solution

Conversation

@rrbharath
Copy link
Copy Markdown

@rrbharath rrbharath commented Apr 9, 2026

Pull Request Template

Description

This PR adds a solution for 51. N-Queens

Problem Description:

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.

Approach
Given its a N x N board we cant bruteforce all possible solutions since that may give us TLE. Instead what we can do is backtrack. We place a queen on each row and if it is safe, we continue otherwise we remove and move onto the next possible correct configuration

Dependencies:
Dart

Complexity
Time Complexity: O(N!)
Space Complexity: O(N)

Difficulty: Hard
Tags: Backtracking, Array

Put check marks:

Have you made changes in README file ?

  • Added problem & solution under correct topic.
  • Specified Space & Time complexity.
  • Specified difficulty level, tag & Note(if any).

How Has This Been Tested?

This solution has been tested by using the following scenarios:

  • Test A
    n = 4
    Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
  • Test B
    n = 1
    Output: [["Q"]]

Make sure all below guidelines are followed else PR will get Reject:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code so that it is easy to understand
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants