Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions 1bfs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
// Function to return Breadth First Traversal of given graph.
vector<int> bfsOfGraph(int V, vector<int> adj[]) {
int vis[V] = {0};
vis[0] = 1;
queue<int> q;
// push the initial starting node
q.push(0);
vector<int> bfs;
// iterate till the queue is empty
while(!q.empty()) {
// get the topmost element in the queue
int node = q.front();
q.pop();
bfs.push_back(node);
// traverse for all its neighbours
for(auto it : adj[node]) {
// if the neighbour has previously not been visited,
// store in Q and mark as visited
if(!vis[it]) {
vis[it] = 1;
q.push(it);
}
}
}
return bfs;
}
};

void addEdge(vector <int> adj[], int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}

void printAns(vector <int> &ans) {
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
}

int main()
{
vector <int> adj[6];

addEdge(adj, 0, 1);
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 0, 4);

Solution obj;
vector <int> ans = obj.bfsOfGraph(5, adj);
printAns(ans);

return 0;
}
60 changes: 60 additions & 0 deletions eggdrop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <bits/stdc++.h>
using namespace std;

// A utility function to get
// maximum of two integers
int max(int a, int b)
{
return (a > b) ? a : b;
}

// Function to get minimum
// number of trials needed in worst
// case with n eggs and k floors
int eggDrop(int n, int k)
{
// If there are no floors,
// then no trials needed.
// OR if there is one floor,
// one trial needed.
if (k == 1 || k == 0)
return k;

// We need k trials for one
// egg and k floors
if (n == 1)
return k;

int min = INT_MAX, x, res;

// Consider all droppings from
// 1st floor to kth floor and
// return the minimum of these
// values plus 1.
for (x = 1; x <= k; x++) {
res = max(
eggDrop(n - 1, x - 1),
eggDrop(n, k - x));
if (res < min)
min = res;
}

return min + 1;
}

// Driver program to test
// to pront printDups
int main()
{
int n = 2, k = 10;
cout << "Minimum number of trials "
"in worst case with "
<< n << " eggs and " << k
<< " floors is "
<< eggDrop(n, k) << endl;
return 0;
}

// This code is contributed
// by Akanksha Rai