GeeksForGeeks - POTD | GFG POTD Answer
Закрытый канал
1 218
Подписчики
Нет данных24 часа
-97 дней
-5730 день
Архив постов
class Solution
{
public:
bool isvalid(int row,int col,int val,int grid[9][9]){
for(int i=0;i<9;i++){
if(grid[row][i]==val)return false;
if(grid[i][col]==val)return false;
if(grid[3*(row/3)+(i/3)][3*(col/3)+(i%3)]==val)return false;
}
return true;
}
//Function to find a solved Sudoku.
bool SolveSudoku(int grid[N][N])
{
// Your code here
for(int i=0;i
class Solution{
public:
//Complete this function
int fact(int n){
int fac=1;
while(n>0){
fac*=n;
n--;
}
return fac;
}
vector permutation(string s)
{
int n=s.size();
int d=fact(n);
vectorvec;
vec.push_back(s);
for(int i=0;i
class Solution{
public:
long long findMinDiff(vector a, long long n, long long m){
sort(a.begin(),a.end());
long long diff=INT_MAX;
for(int i=0;i+m<=n;i++)
diff=min(diff,a[i+m-1]-a[i]);
return diff;
}
};
class Solution{
public:
void Reverse(stack<int> &St){
vector<int>v;
while(!St.empty()){
int a= St.top();
St.pop();
v.push_back(a);
}
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
}
};
class Solution {
public:
vector shortestPath(int n,int m, vector>& edges){
vectordist(n, 1e8);
dist[0] = 0;
for(int i=0; icost+dist[u])
dist[v] = cost+dist[u];
}
for(int val=0; val
class Solution {
public:
int shortestDistance(int N, int M, vector> A, int X, int Y)
{
// We can solve it by using BFS
int ans=0;
vector>vis(N,vector(M,-1));
queue>> q;
vis[0][0]=1;
q.push({0,{0,0}});
vectordelR={-1,0,1,0};
vectordelC={0,1,0,-1};
while(!q.empty())
{
int dist=q.front().first;
int row=q.front().second.first;
int col=q.front().second.second;
if(row==X&&col==Y)
{
return dist;
}
q.pop();
for(int i=0;i<4;i++)
{
int nrow=row+delR[i];
int ncol=col+delC[i];
if(nrow>=0&&nrow=0&&ncol
class Solution {
public:
void dfs(int i,vector &vis,vector &ans,vector adj[]){
if(vis[i]) return;
ans.push_back(i);vis[i]++;
for(int j:adj[i]) dfs(j,vis,ans,adj);
}
// Function to return a list containing the DFS traversal of the graph.
vector dfsOfGraph(int V, vector adj[]) {
vector vis(V,0),ans;
dfs(0,vis,ans,adj);
return ans;
}
};
class Solution {
public:
// Function to return Breadth First Traversal of given graph.
vector bfsOfGraph(int V, vector adj[]) {
vector bfs;
queue q;
vector visited(V, false);
q.push(0);
while(!q.empty()) {
int u=q.front();
q.pop();
if(visited[u]) {
continue;
}
bfs.push_back(u);
visited[u]=true;
for(int v:adj[u]) {
if(visited[v]) {
continue;
}
q.push(v);
}
}
return bfs;
}
};
class Solution{
public:
// returns the inorder successor of the Node x in BST (rooted at 'root')
Node* ans = new Node(-1);
int flag = 0;
void solve(Node* root, Node* x){
if(root == NULL)
return;
solve(root -> left, x);
if(flag == 1){
ans = root;
flag = -1;
}
if(flag == 0 && x == root)
flag = 1;
solve(root -> right, x);
}
Node * inOrderSuccessor(Node *root, Node *x)
{
solve(root, x);
return ans;
}
};
void inorder(Node* root, vector<float> &v){
if(root == NULL)
return;
inorder(root -> left, v);
v.push_back(root -> data);
inorder(root -> right, v);
}
float findMedian(struct Node *root)
{
vector<float> v;
inorder(root, v);
int s = 0;
int e = v.size()-1;
int size = v.size();
int mid = (s+e)/2;
if(size%2 != 0){
return v[mid];
}
else{
return (v[mid+1]+v[mid])/2;
}
}
