Skip to content

added more leetcode solutions #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
106 changes: 106 additions & 0 deletions gfg-solutions/0 - 1 Knapsack Problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// https : // practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1#

// Recursive

class Solution
{
public:
int helper(int W, int wt[], int val[], int n)
{
if (W == 0 or n == -1)
{
return 0;
}

else if (wt[n] > W)
{
return helper(W, wt, val, n - 1);
}

else
{
return max(val[n] + helper(W - wt[n], wt, val, n - 1), helper(W, wt, val, n - 1));
}
}

int knapSack(int W, int wt[], int val[], int n)
{
return helper(W, wt, val, n - 1);
}
};

// Memoization

class Solution
{
public:
int helper(int W, int wt[], int val[], int n, vector<vector<int>> &dp)
{
if (n == 0 or W == 0)
{
return 0;
}

if (dp[n][W] != -1)
{
return dp[n][W];
}

else if (W < wt[n - 1])
{
return dp[n][W] = helper(W, wt, val, n - 1, dp);
}

else
{
return dp[n][W] = max(val[n - 1] + helper(W - wt[n - 1], wt, val, n - 1, dp), helper(W, wt, val, n - 1, dp));
}
}

int knapSack(int W, int wt[], int val[], int n)
{
vector<vector<int>> dp(n + 1, vector<int>(W + 1, -1));

int max_profit = helper(W, wt, val, n, dp);

return max_profit;
}
};

// DP

class Solution
{
public:
int helper(int W, int wt[], int val[], int n, vector<vector<int>> &dp)
{
if (n == 0 or W == 0)
{
return 0;
}

if (dp[n][W] != -1)
{
return dp[n][W];
}

else if (W < wt[n - 1])
{
return dp[n][W] = helper(W, wt, val, n - 1, dp);
}

else
{
return dp[n][W] = max(val[n - 1] + helper(W - wt[n - 1], wt, val, n - 1, dp), helper(W, wt, val, n - 1, dp));
}
}

int knapSack(int W, int wt[], int val[], int n)
{
vector<vector<int>> dp(n + 1, vector<int>(W + 1, -1));

int max_profit = helper(W, wt, val, n, dp);

return max_profit;
}
};
34 changes: 34 additions & 0 deletions gfg-solutions/BFS of graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// https : // practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1

class Solution
{
public:
// Function to return Breadth First Traversal of given graph.
vector<int> bfsOfGraph(int V, vector<int> adj[])
{
vector<int> bfs;
vector<int> vis(V + 1, 0);

if (!vis[0])
{
queue<int> q;
q.push(0);
vis[0] = 1;
while (!q.empty())
{
int node = q.front();
q.pop();
bfs.push_back(node);
for (auto it : adj[node])
{
if (!vis[it])
{
q.push(it);
vis[it] = 1;
}
}
}
}
return bfs;
}
};
32 changes: 32 additions & 0 deletions gfg-solutions/Binary Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https : // practice.geeksforgeeks.org/problems/binary-search/1#

class Solution
{
public:
int binarysearch(int arr[], int n, int k)
{

int s = 0;
int e = n - 1;

while (s <= e)
{
int mid = s + (e - s) / 2;

if (arr[mid] == k)
{
return mid;
}
else if (arr[mid] > k)
{
e = mid - 1;
}
else
{
s = mid + 1;
}
}

return -1;
}
};
64 changes: 64 additions & 0 deletions gfg-solutions/Boolean Parenthesization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// https : // practice.geeksforgeeks.org/problems/boolean-parenthesization5610/1

class Solution
{
public:
unordered_map<string, int> mpp;
int mod = 1003;

int solve(string S, int i, int j, bool is_true)
{
if (i > j)
return 0;
if (i == j)
{
if (is_true == true)
return S[i] == 'T';
else
return S[i] == 'F';
}

string tmp = to_string(i) + " " + to_string(j) + " " + to_string(is_true);
if (mpp.find(tmp) != mpp.end())
return mpp[tmp];
int ans = 0;

for (int k = i + 1; k < j; k = k + 2)
{
int left_true = solve(S, i, k - 1, true);
int right_true = solve(S, k + 1, j, true);
int left_false = solve(S, i, k - 1, false);
int right_false = solve(S, k + 1, j, false);

if (S[k] == '&')
{
if (is_true == true)
ans += left_true * right_true;
else
ans += left_false * right_false + left_true * right_false + right_true * left_false;
}
else if (S[k] == '|')
{
if (is_true == true)
ans += left_true * right_false + right_true * left_false + left_true * right_true;
else
ans += left_false * right_false;
}
else if (S[k] == '^')
{
if (is_true == true)
ans += left_true * right_false + left_false * right_true;
else
ans += left_false * right_false + left_true * right_true;
}
}

return mpp[tmp] = ans % mod;
}

int countWays(int N, string S)
{
mpp.clear();
return solve(S, 0, N - 1, true);
}
};
48 changes: 48 additions & 0 deletions gfg-solutions/Bottom View of Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// https : // practice.geeksforgeeks.org/problems/bottom-view-of-binary-tree/1#

class Solution
{
public:
vector<int> bottomView(Node *root)
{

vector<int> ans;

if (root == NULL)
{
return ans;
}

map<int, int> m;
queue<pair<Node *, int>> q;
q.push({root, 0});

while (!q.empty())
{
pair<Node *, int> p = q.front();
q.pop();

Node *node = p.first;
int vertical = p.second;

m[vertical] = node->data;

if (node->left != NULL)
{
q.push({node->left, vertical - 1});
}

if (node->right != NULL)
{
q.push({node->right, vertical + 1});
}
}

for (auto i : m)
{
ans.push_back(i.second);
}

return ans;
}
};
Loading