-
-
Notifications
You must be signed in to change notification settings - Fork 100
51 solution added #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
51 solution added #207
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #include <bits/stdc++.h> | ||
| using namespace std; | ||
|
|
||
| class Solution { | ||
| public: | ||
| // Function to check if placing a queen at (row, col) is safe | ||
| bool isSafe(int row, int col, vector<string> &temp, int n) { | ||
| int duprow = row; | ||
| int dupcol = col; | ||
| // Check all columns to the left in the same row | ||
| for (int j = 0; j < col; j++) { | ||
| if (temp[row][j] == 'Q') return false; | ||
purrvax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Check upper-left diagonal | ||
| while(row >= 0 && col >= 0){ | ||
| if(temp[row][col] == 'Q') return false; | ||
|
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (performance): Diagonal checks can be replaced with auxiliary arrays for efficiency. Tracking diagonal occupancy with arrays enables constant-time checks, which is more efficient for large boards than iterating through each cell. Suggested implementation: // Function to check if placing a queen at (row, col) is safe using auxiliary arrays
bool isSafe(int row, int col, vector<int> &leftRow, vector<int> &upperDiagonal, vector<int> &lowerDiagonal, int n) {
// Check if the column, upper diagonal, or lower diagonal is occupied
if (leftRow[col] || lowerDiagonal[row + col] || upperDiagonal[n - 1 + col - row])
return false;
return true;
|
||
| row--; | ||
| col--; | ||
| } | ||
| row = duprow; | ||
| col = dupcol; | ||
| // Check lower-left diagonal | ||
| while(row < n && col >= 0){ | ||
| if(temp[row][col] == 'Q') return false; | ||
| row++; | ||
| col--; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Backtracking function to place queens column by column | ||
| void solve(int col, vector<string> temp, vector<vector<string>> &ans, int n) { | ||
| // If all columns are filled, add current board to answer | ||
| if (col == n) { | ||
| ans.push_back(temp); | ||
| return; | ||
| } | ||
|
|
||
| for (int row = 0; row < n; row++) { | ||
| if (isSafe(row, col, temp, n)) { | ||
| // Place queen | ||
| temp[row][col] = 'Q'; | ||
| // Recurse for next column | ||
| solve(col + 1, temp, ans, n); | ||
| // Backtrack and remove queen | ||
| temp[row][col] = '.'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Main function to call backtracking | ||
| vector<vector<string>> solveNQueens(int n) { | ||
| vector<vector<string>> ans; | ||
| vector<string> temp; | ||
| string s(n, '.'); | ||
| for(int i = 0 ; i < n ; i++) temp.push_back(s); // FIXED | ||
|
|
||
| solve(0, temp, ans, n); | ||
| return ans; | ||
| } | ||
|
|
||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Avoid using <bits/stdc++.h> for portability.
This header is not part of the C++ standard and can cause compilation issues on non-GCC compilers. Please include only the specific headers you need.
Suggested implementation:
If your code uses input/output streams (e.g.,
cout,cin), you should also add#include <iostream>. If you use any algorithms (e.g.,sort,find), add#include <algorithm>. Review the rest of your file and include only the headers you actually use.