GEEKS FOR GEEKS SOLUTIONS🫢
Open in Telegram
1 342
Subscribers
No data24 hours
-27 days
-2930 days
Posts Archive
class Solution{
public:
void Reverse(stack<int> &St){
vector<int> arr;
while(St.empty() != true){
arr.push_back(St.top());
St.pop();
}
for(int i = 0 ; i< arr.size() ; i++){
St.push(arr[i]);
}
}
};
class Solution {
public:
vector<int> shortestPath(int N,int M, vector<vector<int>>& edges){
vector<pair<int,int>>adj[N];
for(int i=0;i<M;++i)
{
int u=edges[i][0];
int v=edges[i][1];
int z=edges[i][2];
adj[u].push_back({v,z});
}
vector<int>dist(N);
for(int i=0;i<N;++i)
dist[i]=INT_MAX;
dist[0]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
//{weight,node}
pq.push({0,0});
while(!pq.empty())
{
int node=pq.top().second;
int weight=pq.top().first;
pq.pop();
for(auto it:adj[node])
{
int adjNode=it.first;
int weightage=it.second;
if(weight+weightage<dist[adjNode])
{
dist[adjNode]= weight+weightage;
pq.push({dist[adjNode],adjNode});
}
}
}
for(int i=0;i<dist.size();++i)
{
if(dist[i]==INT_MAX)
dist[i]=-1;
}
return dist;
}
};
class Solution {
public:
int shortestDistance(int N, int M, vector<vector<int>> A, int X, int Y) {
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
pq.push({0, {0, 0}});
vector<vector<int>> vis(N, vector<int>(M, 0));
vis[0][0]=1;
int di[]={0, -1, 0, 1};
int dj[]={-1, 0, 1, 0};
while(!pq.empty()){
int steps = pq.top().first;
int x = pq.top().second.first;
int y = pq.top().second.second;
pq.pop();
if(x==X && y==Y){
return steps;
}
for(int i=0;i<4;i++){
int dx = x + di[i];
int dy = y + dj[i];
if(dx>=0 && dx<N && dy>=0 && dy<M && A[dx][dy]==1 && vis[dx][dy]==0){
vis[dx][dy]=1;
pq.push({steps+1,{dx, dy}});
}
}
}
return -1;
}
};
📢 Attention Immediate Job Seekers! 🚀
🔥 Something BIG is Coming Tomorrow! 🔍👀
🗣 Spread the word and Gather your buddies for this Exciting Opportunity! 🎉🎉🎉
Stay Tuned and Get Ready for Tomorrow's Unveiling! 💼💼💼
class Solution {
public:
// Function to return Breadth First Traversal of given graph.
vector bfsOfGraph(int n, vector g[]) {
vector vis(n, false);
vector v;
queue q;
q.push(0);
vis[0]=1;
while(!q.empty()){
int i = q.front();
v.push_back(i);
q.pop();
for(auto child: g[i]) {
if (vis[child]) continue;
q.push(child);
vis[child]=1;
}
}
return v;
}
};
float findMedian(struct Node *root)
{
queue<Node *> q;
q.push(root);
vector<int> v;
while(!q.empty()){
Node *ptr = q.front();
q.pop();
if (ptr->right!=nullptr) q.push(ptr->right);
if (ptr->left != nullptr) q.push(ptr->left);
v.push_back(ptr->data);
}
sort(v.begin(), v.end());
float n=v.size();
if (v.size()%2==1) return v[(n-1)/2];
return (v[n/2] + v[n/2-1])/2.0;
}
void helper(Node* root,stack<int> &st,int node){
if(root == NULL) return;
st.push(root->data);
helper(root->left,st,node);
helper(root->right,st,node);
if(st.top() != node) st.pop();
}
int kthAncestor(Node *root, int k, int node)
{
stack<int> st;
helper(root,st,node);
int ans=-1;
while(!st.empty())
{
if(k==0) ans=st.top();
st.pop();
k--;
}
return ans;
}
vector findSpiral(Node *root)
{
vectorans;
dequedq;
dq.push_back(root);
char dir='l';
dequedq1;
while(!dq.empty()){
Node*t = dq.front();
dq.pop_front();
ans.push_back(t->data);
if(dir=='l'){
if(t->right){dq1.push_front(t->right);}
if(t->left){dq1.push_front(t->left);}
}
else{
if(t->left){dq1.push_front(t->left);}
if(t->right){dq1.push_front(t->right);}
}
if(dq.empty()){
dq=dq1;
if(dir=='l'){
dir='r';
}
else{
dir='l';
}
dq1.clear();
}
}
return ans;
}
