Programming Question — N Queens , Leetcode 51
Approach to solve leetcode question — N-Queens
- 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.
- 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.
- 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.
- 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.
- lastly , we will convert queens into board as the positions where queens exist , insert ‘Q’ there otherwise insert ‘.’
- we will return result.
Code :
