Programming Question — N Queens , Leetcode 51

  1. In this question, we are asked to find ways in which n queens can be placed in a n*n board without attacking each other.
  2. we will take a helper function in which we will pass rows staring from 0 and for each column we will see the position is safe for the queen or not.
  3. if the position is safe, we will add the position(row and column) to our queen set, and recursively call helper function for next row, and recursively remove the position from queens.
  4. for safety of any row and column, we will check the row and column are present in the queen set or not, if present, we will return false.
  5. lastly , we will convert queens into board as the positions where queens exist , insert ‘Q’ there otherwise insert ‘.’
  6. we will return result.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

HTB Traceback — Walkthrough

Part 2: GraphQL with Node and Express

A brief introduction of SCSS/SASS

A brief introduction to SCSS/SASS

Part 9. Continuous attention to technical excellence and good design, and… 10. Simplicity

Publish your first Web Application in AWS

Continuously deploy ARM Templates using Azure DevOps

How is hash collision handled in HashMap?

Dynamic Programming

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Raj shukla

Raj shukla


More from Medium

Sort Array By Parity — LeetCode

LeetCode — Minimum Difficulty of a Job Schedule

Linked List and its Time Complexities

[Leetcode 525] Contiguous Array