You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The n-queens puzzle is the problem of placing n queens on a (n×n) chessboard such that no two queens can attack each other.
4
+
Given an integer n, find all distinct solutions to the n-queens puzzle. Each solution contains distinct board configurations of the n-queens placement, where the solutions are a permutation of [1,2,3..n] in increasing order, here the number in the ith place denotes that the ith-column queen is placed in the row with that number. For eg below figure represents a chessboard [3 1 4 2].
5
+
Examples
6
+
Input: 1
7
+
Output: [1]
8
+
Explaination: Only one queen can be placed in the single cell available.
9
+
Input: 4
10
+
Output: [[2 4 1 3 ],[3 1 4 2 ]]
11
+
Explaination: These are the 2 possible solutions.
12
+
Expected Time Complexity: O(n!)
13
+
Expected Auxiliary Space: O(n2)
14
+
Constraints:
15
+
1 ≤ n ≤ 10
16
+
*/
17
+
// User function Template for C++
18
+
19
+
classSolution{
20
+
public:
21
+
vector<vector<int>> res;
22
+
boolcheck( int x, int y, int n, vector<vector<int>> &chessBoard ){
23
+
int i, j;
24
+
i = x-1; j = y;
25
+
while ( i >= 0 ) if ( chessBoard[i--][j] == 1 ) returnfalse;
26
+
i = x+1; j = y;
27
+
while ( i < n ) if ( chessBoard[i++][j] == 1 ) returnfalse;
28
+
i = x; j = y-1;
29
+
while ( j >= 0 ) if ( chessBoard[i][j--] == 1 ) returnfalse;
30
+
i = x-1; j = y-1;
31
+
while ( i >= 0 && j >= 0 ) if ( chessBoard[i--][j--] == 1 ) returnfalse;
32
+
i = x+1; j = y-1;
33
+
while ( i < n && j >= 0 ) if ( chessBoard[i++][j--] == 1 ) returnfalse;
34
+
returntrue;
35
+
}
36
+
voidrec( int j, int n, vector<int> &pos, vector<vector<int>> &chessBoard ){
You are given an undirected weighted connected graph containing n nodes labeled from 0 to n - 1, and an integer array edges where edges[i] = [ai, bi, wi] indicates that there is an edge between nodes ai and bi with weight wi.
4
+
Some edges have a weight of -1 (wi = -1), while others have a positive weight (wi > 0).
5
+
Your task is to modify all edges with a weight of -1 by assigning them positive integer values in the range [1, 2 * 109] so that the shortest distance between the nodes source and destination becomes equal to an integer target. If there are multiple modifications that make the shortest distance between source and destination equal to target, any of them will be considered correct.
6
+
Return an array containing all edges (even unmodified ones) in any order if it is possible to make the shortest distance from source to destination equal to target, or an empty array if it's impossible.
7
+
Note: You are not allowed to modify the weights of edges with initial positive weights.
Explanation: The graph above contains the initial edges. It is not possible to make the distance from 0 to 2 equal to 6 by modifying the edge with weight -1. So, an empty array is returned.
The n-queens puzzle is the problem of placing n queens on a (n×n) chessboard such that no two queens can attack each other.
4
+
Given an integer n, find all distinct solutions to the n-queens puzzle. Each solution contains distinct board configurations of the n-queens placement, where the solutions are a permutation of [1,2,3..n] in increasing order, here the number in the ith place denotes that the ith-column queen is placed in the row with that number. For eg below figure represents a chessboard [3 1 4 2].
5
+
Examples
6
+
Input: 1
7
+
Output: [1]
8
+
Explaination: Only one queen can be placed in the single cell available.
9
+
Input: 4
10
+
Output: [[2 4 1 3 ],[3 1 4 2 ]]
11
+
Explaination: These are the 2 possible solutions.
12
+
Expected Time Complexity: O(n!)
13
+
Expected Auxiliary Space: O(n2)
14
+
Constraints:
15
+
1 ≤ n ≤ 10
16
+
*/
17
+
// User function Template for C++
18
+
19
+
classSolution{
20
+
public:
21
+
vector<vector<int>> res;
22
+
boolcheck( int x, int y, int n, vector<vector<int>> &chessBoard ){
23
+
int i, j;
24
+
i = x-1; j = y;
25
+
while ( i >= 0 ) if ( chessBoard[i--][j] == 1 ) returnfalse;
26
+
i = x+1; j = y;
27
+
while ( i < n ) if ( chessBoard[i++][j] == 1 ) returnfalse;
28
+
i = x; j = y-1;
29
+
while ( j >= 0 ) if ( chessBoard[i][j--] == 1 ) returnfalse;
30
+
i = x-1; j = y-1;
31
+
while ( i >= 0 && j >= 0 ) if ( chessBoard[i--][j--] == 1 ) returnfalse;
32
+
i = x+1; j = y-1;
33
+
while ( i < n && j >= 0 ) if ( chessBoard[i++][j--] == 1 ) returnfalse;
34
+
returntrue;
35
+
}
36
+
voidrec( int j, int n, vector<int> &pos, vector<vector<int>> &chessBoard ){
You are given an undirected weighted connected graph containing n nodes labeled from 0 to n - 1, and an integer array edges where edges[i] = [ai, bi, wi] indicates that there is an edge between nodes ai and bi with weight wi.
4
+
Some edges have a weight of -1 (wi = -1), while others have a positive weight (wi > 0).
5
+
Your task is to modify all edges with a weight of -1 by assigning them positive integer values in the range [1, 2 * 109] so that the shortest distance between the nodes source and destination becomes equal to an integer target. If there are multiple modifications that make the shortest distance between source and destination equal to target, any of them will be considered correct.
6
+
Return an array containing all edges (even unmodified ones) in any order if it is possible to make the shortest distance from source to destination equal to target, or an empty array if it's impossible.
7
+
Note: You are not allowed to modify the weights of edges with initial positive weights.
Explanation: The graph above contains the initial edges. It is not possible to make the distance from 0 to 2 equal to 6 by modifying the edge with weight -1. So, an empty array is returned.
0 commit comments