**LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Problems C++**

Hello **Programmers/Coders,** Today we are going to share ** solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python**. At Each Problem with Successful submission with

**all Test Cases Passed,**you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the **N-Queens II** **in C++, Java & Python-LeetCode problem**. We are providing the **correct and tested solutions** to coding problems present on **LeetCode**. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

**About LeetCode**

*LeetCode* is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.

LeetCode is for **software engineers who are looking to practice technical questions and advance their skills**. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either *Easy*, *Medium*, or *Hard*.

**LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:**

- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

** Link for the Problem** – N-Queens II– LeetCode Problem

N-Queens II– LeetCode Problem

**Problem:**

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 *the number of distinct solutions to the n-queens puzzle*.

**Example 1:**

Input:n = 4Output:2Explanation:There are two distinct solutions to the 4-queens puzzle as shown.

**Example 2:**

Input:n = 1Output:1

**Constraints:**

N-Queens II– LeetCode Solutions

N-Queens II in C++:

class Solution { public: int totalNQueens(int n) { int ans = 0; dfs(n, 0, vector(n), vector (2 * n - 1), vector (2 * n - 1), ans); return ans; } private: void dfs(int n, int i, vector && cols, vector && diag1, vector && diag2, int& ans) { if (i == n) { ++ans; return; } for (int j = 0; j < n; ++j) { if (cols[j] || diag1[i + j] || diag2[j - i + n - 1]) continue; cols[j] = diag1[i + j] = diag2[j - i + n - 1] = true; dfs(n, i + 1, move(cols), move(diag1), move(diag2), ans); cols[j] = diag1[i + j] = diag2[j - i + n - 1] = false; } } };

N-Queens II in Java:

class Solution { public int totalNQueens(int n) { dfs(n, 0, new boolean[n], new boolean[2 * n - 1], new boolean[2 * n - 1]); return ans; } private int ans = 0; private void dfs(int n, int i, boolean[] cols, boolean[] diag1, boolean[] diag2) { if (i == n) { ++ans; return; } for (int j = 0; j < cols.length; ++j) { if (cols[j] || diag1[i + j] || diag2[j - i + n - 1]) continue; cols[j] = diag1[i + j] = diag2[j - i + n - 1] = true; dfs(n, i + 1, cols, diag1, diag2); cols[j] = diag1[i + j] = diag2[j - i + n - 1] = false; } } }

N-Queens II in Python:

class Solution: def totalNQueens(self, n: int) -> int: self.ans = 0 cols = [False] * n diag1 = [False] * (2 * n - 1) diag2 = [False] * (2 * n - 1) def dfs(i: int) -> None: if i == n: self.ans += 1 return for j in range(n): if cols[j] or diag1[i + j] or diag2[j - i + n - 1]: continue cols[j] = diag1[i + j] = diag2[j - i + n - 1] = True dfs(i + 1) cols[j] = diag1[i + j] = diag2[j - i + n - 1] = False dfs(0) return self.ans