Skip to content
Open
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
49 changes: 49 additions & 0 deletions DSA/Dynamic Programming/super_egg_drop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <bits/stdc++.h>

using namespace std;



int minTrials(int n, int k)
{
int dp[n+1][k+1], res;
int i,j,x;

for (i = 1; i <= n; i++)
{
dp[i][1] = 1;
dp[i][0] = 0;
}

for (j = 1; j <= k; j++)
dp[1][j] = j;


for (i = 2; i <= n; i++)
{
for (j = 2; j <= k; j++)
{
dp[i][j] = INT_MAX;
for (x = 1; x <= j; x++)
{
res = 1 + max(dp[i-1][x-1], dp[i][j-x]);
if (res < dp[i][j])
dp[i][j] = res;
}
}
}

return dp[n][k];

}
int main()
{

int t,n,k;
cin>>t;
while(t--)
{
cin>>n>>k;
cout<<minTrials(n,k)<<endl;
}
}
124 changes: 124 additions & 0 deletions DSA/Graph/binary maze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// C++ program to find the shortest path between
// a given source cell to a destination cell.
#include <bits/stdc++.h>
using namespace std;
#define ROW 9
#define COL 10

//To store matrix cell cordinates
struct Point
{
int x;
int y;
};

// A Data Structure for queue used in BFS
struct queueNode
{
Point pt; // The cordinates of a cell
int dist; // cell's distance of from the source
};

// check whether given cell (row, col) is a valid
// cell or not.
bool isValid(int row, int col)
{
// return true if row number and column number
// is in range
return (row >= 0) && (row < ROW) &&
(col >= 0) && (col < COL);
}

// These arrays are used to get row and column
// numbers of 4 neighbours of a given cell
int rowNum[] = {-1, 0, 0, 1};
int colNum[] = {0, -1, 1, 0};

// function to find the shortest path between
// a given source cell to a destination cell.
int BFS(int mat[][COL], Point src, Point dest)
{
// check source and destination cell
// of the matrix have value 1
if (!mat[src.x][src.y] || !mat[dest.x][dest.y])
return -1;

bool visited[ROW][COL];
memset(visited, false, sizeof visited);

// Mark the source cell as visited
visited[src.x][src.y] = true;

// Create a queue for BFS
queue<queueNode> q;

// Distance of source cell is 0
queueNode s = {src, 0};
q.push(s); // Enqueue source cell

// Do a BFS starting from source cell
while (!q.empty())
{
queueNode curr = q.front();
Point pt = curr.pt;

// If we have reached the destination cell,
// we are done
if (pt.x == dest.x && pt.y == dest.y)
return curr.dist;

// Otherwise dequeue the front cell in the queue
// and enqueue its adjacent cells
q.pop();

for (int i = 0; i < 4; i++)
{
int row = pt.x + rowNum[i];
int col = pt.y + colNum[i];

// if adjacent cell is valid, has path and
// not visited yet, enqueue it.
if (isValid(row, col) && mat[row][col] &&
!visited[row][col])
{
// mark cell as visited and enqueue it
visited[row][col] = true;
queueNode Adjcell = { {row, col},
curr.dist + 1 };
q.push(Adjcell);
}
}
}

// Return -1 if destination cannot be reached
return -1;
}

// Driver program to test above function
int main()
{
int mat[ROW][COL] =
{
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }
};

Point source = {0, 0};
Point dest = {3, 4};

int dist = BFS(mat, source, dest);

if (dist != INT_MAX)
cout << "Shortest Path is " << dist ;
else
cout << "Shortest Path doesn't exist";

return 0;
}
30 changes: 30 additions & 0 deletions DSA/Graph/number_of_connected_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

def adjacency(adj, u ,v):
adj[u].append(v)
adj[v].append(u)

def DFSUtil(adj, visited, s):
visited[s] = True
for j in range(len(adj[s])):
if visited[adj[s][j]]==False:
DFSUtil(adj, visited, adj[s][j])

def DFS(adj,V):
visited=[False for i in range(V)]
count=0
for i in range(V):
if visited[i] == False:
DFSUtil(adj,visited,i)
count+=1
return count

if __name__ == '__main__':
V=5
adj = [[] for i in range(V)]

adjacency(adj,1,0)
adjacency(adj,2,3)
adjacency(adj,3,4)

print DFS(adj, V)

74 changes: 74 additions & 0 deletions DSA/Linked Lists/detect_and_remove_loop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
struct Node* next;
};

void push(struct Node** head_ref, int new_data)
{

struct Node* new_node = new Node;


new_node->data = new_data;


new_node->next = (*head_ref);


(*head_ref) = new_node;
}


void loopremover(Node* head)
{

unordered_map<Node*, int> node_map;

Node* last = NULL;
while (head != NULL) {

if (node_map.find(head) == node_map.end()) {
node_map[head]++;
last = head;
head = head->next;
}

else {
last->next = NULL;
break;
}
}
}
void printList(Node* head)
{
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
}


int main()
{

struct Node* head = NULL;

push(&head, 7);
push(&head, 4);
push(&head, 5);
push(&head, 10);

head->next->next->next->next = head;
//printList(head);
loopremover(head);

printf("Linked List after removing loop \n");
printList(head);

return 0;
}

82 changes: 82 additions & 0 deletions DSA/Linked Lists/merge and sort sorted sll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <bits/stdc++.h>

using namespace std;

class node
{
public:
int data;
node* next;
};

void print(node* head)
{
while(head != NULL)
{
cout<<head->data;
head = head->next;
}
}

node* linkmerge(node* a, node *b)
{ node *result = NULL;
if (a==NULL)
return b;
else if (b==NULL)
return a;

if (a->data<=b->data)
{
cout<<"kya hora";
result = a;
result->next = linkmerge(a->next, b);
}
else
{
cout<<"kcuh ini";
result = b;
result->next = linkmerge(a, b->next);
}
}
int main()
{
node* first = NULL;
node* second = NULL;
node* third = NULL;

first = new node;
second = new node;
third = new node;

first->data = 1;
first->next = second;
second->data = 7; // assign data to second node
second->next = third;

third->data = 14; // assign data to third node
third->next = NULL;

node* first2 = NULL;
node* second2 = NULL;
node* third2 = NULL;

first2 = new node;
second2 = new node;
third2 = new node;

first2->data = 4;
first2->next = second2;
second2->data = 6; // assign data to second node
second2->next = third2;

third2->data = 9; // assign data to third node
third2->next = NULL;
print(first2);
print(first);
node* a = NULL;
a = linkmerge(first,first2);
print(a);

return 0;

}
Loading