Skip to content

Commit 42d4fc2

Browse files
POTD 30-Aug-2024
1 parent f4ec359 commit 42d4fc2

File tree

4 files changed

+312
-0
lines changed

4 files changed

+312
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
N-Queen Problem
3+
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+
class Solution{
20+
public:
21+
vector<vector<int>> res;
22+
bool check( 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 ) return false;
26+
i = x+1; j = y;
27+
while ( i < n ) if ( chessBoard[i++][j] == 1 ) return false;
28+
i = x; j = y-1;
29+
while ( j >= 0 ) if ( chessBoard[i][j--] == 1 ) return false;
30+
i = x-1; j = y-1;
31+
while ( i >= 0 && j >= 0 ) if ( chessBoard[i--][j--] == 1 ) return false;
32+
i = x+1; j = y-1;
33+
while ( i < n && j >= 0 ) if ( chessBoard[i++][j--] == 1 ) return false;
34+
return true;
35+
}
36+
void rec( int j, int n, vector<int> &pos, vector<vector<int>> &chessBoard ){
37+
if ( j == n ){
38+
res.push_back(pos);
39+
return;
40+
}
41+
for ( int i = 0; i<n; i++ ){
42+
if ( check(i, j, n, chessBoard) ) {
43+
pos.push_back(i+1);
44+
chessBoard[i][j] = 1;
45+
rec(j+1, n, pos, chessBoard);
46+
chessBoard[i][j] = 0;
47+
pos.pop_back();
48+
}
49+
}
50+
}
51+
vector<vector<int>> nQueen(int n) {
52+
vector<vector<int>> chessBoard(n+1, vector<int>(n+1, 0));
53+
vector<int> pos; rec(0, n, pos, chessBoard);
54+
return res;
55+
}
56+
};
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
2699. Modify Graph Edge Weights
3+
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.
8+
Example 1:
9+
Input: n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5
10+
Output: [[4,1,1],[2,0,1],[0,3,3],[4,3,1]]
11+
Explanation: The graph above shows a possible modification to the edges, making the distance from 0 to 1 equal to 5.
12+
Example 2:
13+
Input: n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6
14+
Output: []
15+
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.
16+
Example 3:
17+
Input: n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6
18+
Output: [[1,0,4],[1,2,3],[2,3,5],[0,3,1]]
19+
Explanation: The graph above shows a modified graph having the shortest distance from 0 to 2 as 6.
20+
Constraints:
21+
1 <= n <= 100
22+
1 <= edges.length <= n * (n - 1) / 2
23+
edges[i].length == 3
24+
0 <= ai, bi < n
25+
wi = -1 or 1 <= wi <= 107
26+
ai != bi
27+
0 <= source, destination < n
28+
source != destination
29+
1 <= target <= 109
30+
The graph is connected, and there are no self-loops or repeated edges
31+
*/
32+
class Solution {
33+
public:
34+
vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int source, int destination, int target) {
35+
int INF = 2e9;
36+
unordered_map<int, vector<pair<int, int>>> nodeNeighMp;
37+
38+
for (auto& edge : edges) {
39+
int firstNode = edge[0], secondNode = edge[1], edgWt = edge[2];
40+
if (edgWt == -1) continue;
41+
nodeNeighMp[firstNode].emplace_back(secondNode, edgWt);
42+
nodeNeighMp[secondNode].emplace_back(firstNode, edgWt);
43+
}
44+
45+
int srcDestDist = findShortestDist(n, source, destination, nodeNeighMp);
46+
47+
if (srcDestDist < target) {
48+
return {};
49+
}
50+
51+
bool distEqToTrgt = srcDestDist == target;
52+
53+
for (int indx = 0; indx < edges.size(); indx++) {
54+
auto& edge = edges[indx];
55+
int firstNode = edge[0], secondNode = edge[1], edgWt = edge[2];
56+
if (edgWt != -1) continue;
57+
58+
edgWt = distEqToTrgt ? INF : 1;
59+
nodeNeighMp[firstNode].emplace_back(secondNode, edgWt);
60+
nodeNeighMp[secondNode].emplace_back(firstNode, edgWt);
61+
62+
if (!distEqToTrgt) {
63+
srcDestDist = findShortestDist(n, source, destination, nodeNeighMp);
64+
if (srcDestDist <= target) {
65+
edgWt += (target - srcDestDist);
66+
distEqToTrgt = true;
67+
}
68+
}
69+
70+
edge[2] = edgWt;
71+
}
72+
73+
return distEqToTrgt ? edges : vector<vector<int>>{};
74+
}
75+
76+
private:
77+
int findShortestDist(int n, int startNode, int endNode, unordered_map<int, vector<pair<int, int>>>& nodeNeighMp) {
78+
int INF = 2e9;
79+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minDisFrmSrcHeap;
80+
vector<int> distFrmSrc(n, INF);
81+
distFrmSrc[startNode] = 0;
82+
minDisFrmSrcHeap.emplace(distFrmSrc[startNode], startNode);
83+
84+
while (!minDisFrmSrcHeap.empty()) {
85+
auto nodePr = minDisFrmSrcHeap.top();
86+
minDisFrmSrcHeap.pop();
87+
int nodeVal = nodePr.second, nodeDisFrmSrc = nodePr.first;
88+
if (!nodeNeighMp.count(nodeVal)) continue;
89+
for (auto& neighPr : nodeNeighMp[nodeVal]) {
90+
int neighVal = neighPr.first, neighNodeDist = neighPr.second;
91+
if (neighNodeDist + nodeDisFrmSrc < distFrmSrc[neighVal]) {
92+
distFrmSrc[neighVal] = neighNodeDist + nodeDisFrmSrc;
93+
minDisFrmSrcHeap.emplace(distFrmSrc[neighVal], neighVal);
94+
}
95+
}
96+
}
97+
98+
return distFrmSrc[endNode];
99+
}
100+
};

GFG/HARD/N_Queen_Problem.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
N-Queen Problem
3+
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+
class Solution{
20+
public:
21+
vector<vector<int>> res;
22+
bool check( 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 ) return false;
26+
i = x+1; j = y;
27+
while ( i < n ) if ( chessBoard[i++][j] == 1 ) return false;
28+
i = x; j = y-1;
29+
while ( j >= 0 ) if ( chessBoard[i][j--] == 1 ) return false;
30+
i = x-1; j = y-1;
31+
while ( i >= 0 && j >= 0 ) if ( chessBoard[i--][j--] == 1 ) return false;
32+
i = x+1; j = y-1;
33+
while ( i < n && j >= 0 ) if ( chessBoard[i++][j--] == 1 ) return false;
34+
return true;
35+
}
36+
void rec( int j, int n, vector<int> &pos, vector<vector<int>> &chessBoard ){
37+
if ( j == n ){
38+
res.push_back(pos);
39+
return;
40+
}
41+
for ( int i = 0; i<n; i++ ){
42+
if ( check(i, j, n, chessBoard) ) {
43+
pos.push_back(i+1);
44+
chessBoard[i][j] = 1;
45+
rec(j+1, n, pos, chessBoard);
46+
chessBoard[i][j] = 0;
47+
pos.pop_back();
48+
}
49+
}
50+
}
51+
vector<vector<int>> nQueen(int n) {
52+
vector<vector<int>> chessBoard(n+1, vector<int>(n+1, 0));
53+
vector<int> pos; rec(0, n, pos, chessBoard);
54+
return res;
55+
}
56+
};
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
2699. Modify Graph Edge Weights
3+
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.
8+
Example 1:
9+
Input: n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5
10+
Output: [[4,1,1],[2,0,1],[0,3,3],[4,3,1]]
11+
Explanation: The graph above shows a possible modification to the edges, making the distance from 0 to 1 equal to 5.
12+
Example 2:
13+
Input: n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6
14+
Output: []
15+
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.
16+
Example 3:
17+
Input: n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6
18+
Output: [[1,0,4],[1,2,3],[2,3,5],[0,3,1]]
19+
Explanation: The graph above shows a modified graph having the shortest distance from 0 to 2 as 6.
20+
Constraints:
21+
1 <= n <= 100
22+
1 <= edges.length <= n * (n - 1) / 2
23+
edges[i].length == 3
24+
0 <= ai, bi < n
25+
wi = -1 or 1 <= wi <= 107
26+
ai != bi
27+
0 <= source, destination < n
28+
source != destination
29+
1 <= target <= 109
30+
The graph is connected, and there are no self-loops or repeated edges
31+
*/
32+
class Solution {
33+
public:
34+
vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int source, int destination, int target) {
35+
int INF = 2e9;
36+
unordered_map<int, vector<pair<int, int>>> nodeNeighMp;
37+
38+
for (auto& edge : edges) {
39+
int firstNode = edge[0], secondNode = edge[1], edgWt = edge[2];
40+
if (edgWt == -1) continue;
41+
nodeNeighMp[firstNode].emplace_back(secondNode, edgWt);
42+
nodeNeighMp[secondNode].emplace_back(firstNode, edgWt);
43+
}
44+
45+
int srcDestDist = findShortestDist(n, source, destination, nodeNeighMp);
46+
47+
if (srcDestDist < target) {
48+
return {};
49+
}
50+
51+
bool distEqToTrgt = srcDestDist == target;
52+
53+
for (int indx = 0; indx < edges.size(); indx++) {
54+
auto& edge = edges[indx];
55+
int firstNode = edge[0], secondNode = edge[1], edgWt = edge[2];
56+
if (edgWt != -1) continue;
57+
58+
edgWt = distEqToTrgt ? INF : 1;
59+
nodeNeighMp[firstNode].emplace_back(secondNode, edgWt);
60+
nodeNeighMp[secondNode].emplace_back(firstNode, edgWt);
61+
62+
if (!distEqToTrgt) {
63+
srcDestDist = findShortestDist(n, source, destination, nodeNeighMp);
64+
if (srcDestDist <= target) {
65+
edgWt += (target - srcDestDist);
66+
distEqToTrgt = true;
67+
}
68+
}
69+
70+
edge[2] = edgWt;
71+
}
72+
73+
return distEqToTrgt ? edges : vector<vector<int>>{};
74+
}
75+
76+
private:
77+
int findShortestDist(int n, int startNode, int endNode, unordered_map<int, vector<pair<int, int>>>& nodeNeighMp) {
78+
int INF = 2e9;
79+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minDisFrmSrcHeap;
80+
vector<int> distFrmSrc(n, INF);
81+
distFrmSrc[startNode] = 0;
82+
minDisFrmSrcHeap.emplace(distFrmSrc[startNode], startNode);
83+
84+
while (!minDisFrmSrcHeap.empty()) {
85+
auto nodePr = minDisFrmSrcHeap.top();
86+
minDisFrmSrcHeap.pop();
87+
int nodeVal = nodePr.second, nodeDisFrmSrc = nodePr.first;
88+
if (!nodeNeighMp.count(nodeVal)) continue;
89+
for (auto& neighPr : nodeNeighMp[nodeVal]) {
90+
int neighVal = neighPr.first, neighNodeDist = neighPr.second;
91+
if (neighNodeDist + nodeDisFrmSrc < distFrmSrc[neighVal]) {
92+
distFrmSrc[neighVal] = neighNodeDist + nodeDisFrmSrc;
93+
minDisFrmSrcHeap.emplace(distFrmSrc[neighVal], neighVal);
94+
}
95+
}
96+
}
97+
98+
return distFrmSrc[endNode];
99+
}
100+
};

0 commit comments

Comments
 (0)