Skip to content

Implement N Queens Solution Dart#538

Merged
GouravRusiya30 merged 2 commits into
codedecks-in:masterfrom
rrbharath:n-queens-dart-solution
Jun 6, 2026
Merged

Implement N Queens Solution Dart#538
GouravRusiya30 merged 2 commits into
codedecks-in:masterfrom
rrbharath:n-queens-dart-solution

Conversation

@rrbharath
Copy link
Copy Markdown
Contributor

@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

@GouravRusiya30
Copy link
Copy Markdown
Member

Thanks for the contribution! The solution looks correct and the README entry has been added.

One minor suggestion: the backtrack() method includes a col parameter that is never used. Inside the function, a local loop variable named col shadows it:

void backtrack(
List<List> board,
List<List> ans,
int row,
int col,
int queens
)

Since the parameter is unused, it can be removed to simplify the method signature and improve readability.

Also, consider adding a trailing newline at the end of the file to match common repository conventions.

I verified that the README has been updated with an entry for the N-Queens Dart solution, so that requirement is satisfied.

Overall, the backtracking implementation looks correct. Nice work!

@GouravRusiya30 GouravRusiya30 merged commit 947710a into codedecks-in:master Jun 6, 2026
@welcome
Copy link
Copy Markdown

welcome Bot commented Jun 6, 2026

Your code looks great! Congrats, I've gone ahead and merged your first pull request! Keep it up! alt text

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.

3 participants