Skip to content

Commit

Permalink
resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Aug 26, 2021
2 parents bb4c947 + d3d9350 commit e41a63f
Show file tree
Hide file tree
Showing 24 changed files with 584 additions and 27 deletions.
8 changes: 3 additions & 5 deletions BST/check_if_BST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ bool isBSTUtil(Node *root, int min, int max)
{
if (root == nullptr)
return true;
if (root->data > min && root->data < max)
{
return isBSTUtil(root->left, min, root->data) && isBSTUtil(root->right, root->data, max);
}
return false;
if (root->data < min || root->data > max)
return false;
return isBSTUtil(root->left, min, root->data - 1) && isBSTUtil(root->right, root->data + 1, max);
}

bool isBST(Node *root)
Expand Down
10 changes: 9 additions & 1 deletion Disjoint Set/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using namespace std;

int *parent;

int find(int x)
{
if (parent[x] == x)
Expand All @@ -14,7 +15,14 @@ void _union(int a, int b)
{
a = find(a);
b = find(b);
parent[a] = b;
if (a == b)
{
return;
}
else
{
parent[a] = b;
}
}

vector<int> findRedundantConnection(vector<vector<int>> &edges)
Expand Down
Binary file removed Disjoint Set/teacher_dilemma
Binary file not shown.
Binary file removed Disjoint Set/union_compression
Binary file not shown.
20 changes: 8 additions & 12 deletions Dynamic Programming/LCS_based/edit_distance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ int minDistance(string x, string y)
int m = x.length();
int n = y.length();
int t[m + 1][n + 1];
t[0][0] = 0;
for (int i = 1; i <= m; i++)
{
t[i][0] = i;
}
for (int j = 1; j <= n; j++)
{
t[0][j] = j;
}
for (int i = 1; i <= m; i++)

for (int i = 0; i <= m; i++)
{
for (int j = 1; j <= n; j++)
for (int j = 0; j <= n; j++)
{
if (x[i - 1] == y[j - 1])
if (i == 0)
t[i][j] = j;
else if (j == 0)
t[i][j] = i;
else if (x[i - 1] == y[j - 1])
{
t[i][j] = t[i - 1][j - 1];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bits/stdc++.h>
using namespace std;

//insertion deletion same hi hogaaa
// **insertion deletion same hi hogaaa
int findLCS(string x, string y, int m, int n)
{
int t[m + 1][n + 1];
Expand Down Expand Up @@ -35,5 +35,6 @@ int main()
{
y += x[i];
}
// **insertion deletion same hi hogaaa
cout << x.length() - findLCS(x, y, x.length(), y.length()) << endl;
}
2 changes: 1 addition & 1 deletion Graph/mininum_distance_between_two_nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void bfs(vector<vector<int>> &adj, vector<bool> &vis, vector<int> &dis, int i)
if (!vis[j])
{
q.push(j);
dis[j] += (dis[temp] + 1);
dis[j] = (dis[temp] + 1);
vis[j] = true;
}
}
Expand Down
5 changes: 2 additions & 3 deletions Graph/topological_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bits/stdc++.h>
using namespace std;

void kahn(vector<vector<int>> &adj, vector<bool> &vis, vector<int> &in, vector<int> &res, int n)
void kahn(vector<vector<int>> &adj, vector<int> &in, vector<int> &res, int n)
{
priority_queue<int, vector<int>, greater<int>> qu;
for (int i = 1; i <= n; i++)
Expand Down Expand Up @@ -43,7 +43,6 @@ int main()
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n + 1);
vector<bool> vis(n + 1, false);
vector<int> in(n + 1, 0);
vector<int> res;
for (int i = 0; i < m; i++)
Expand All @@ -53,5 +52,5 @@ int main()
adj[a].push_back(b);
in[b]++;
}
kahn(adj, vis, in, res, n);
kahn(adj, in, res, n);
}
1 change: 0 additions & 1 deletion Grid/dfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ int dy[] = {0, 1, 0, -1};
void dfs(int x, int y, int n, int m)
{
vis[x][y] = 1;
cout << x << " " << y << endl;
for (int i = 0; i < 4; i++)
{
if (isValid(x + dx[i], y + dy[i], n, m))
Expand Down
22 changes: 22 additions & 0 deletions Matrix/rotate_matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> rotate(vector<vector<int>> &matrix)
{
int n = matrix.size();
int m = matrix[0].size();
if (n == 0)
return {};
else
{
reverse(matrix.begin(), matrix.end());
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < m; j++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
return matrix;
}
}
29 changes: 29 additions & 0 deletions Matrix/rows_with_max_ones.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;

int rowWithMax1(vector<vector<int>> &matrix)
{
int n = matrix.size();
int m = matrix[0].size();
if (n == 0)
return 0;
else
{
int row = -1;
int i = 0;
int j = m - 1;
while (i <= n && j >= 0)
{
if (matrix[i][j] == 1)
{
row = i;
j--;
}
else
{
i++;
}
}
return row;
}
}
30 changes: 30 additions & 0 deletions Matrix/search_in_2d_matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

bool searchMatrix(vector<vector<int>> &arr, int target)
{
if (arr.size() == 0)
return false;
else
{
int n = arr.size();
int m = arr[0].size();
int low = 0, high = m - 1;
while (low < n && high >= 0)
{
if (arr[low][high] == target)
{
return true;
}
else if (arr[low][high] > target)
{
high--;
}
else
{
low++;
}
}
return false;
}
}
55 changes: 55 additions & 0 deletions Sliding Window/Fixed/count_occurances_of_anagram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <bits/stdc++.h>
using namespace std;

int solve(string s, string pat)
{
int n = s.length();
int i = 0;
int j = 0;
int k = pat.length();
int ans = 0;
map<char, int> mp;
for (auto j : pat)
{
mp[j]++;
}
int count = mp.size();
while (j < n)
{
// calculation
if (mp.find(s[j]) != mp.end())
{
mp[s[j]]--;
if (mp[s[j]] == 0)
{
count--;
}
}

// less than k
if ((j - i + 1) < k)
{
j++;
}
// equal to k
else if ((j - i + 1) == k)
{
if (count == 0)
{
ans++;
}
// nullify the start
if (mp.find(s[i]) != mp.end())
{
mp[s[i]]++;
if (mp[s[i]] == 1)
{
count++;
}
}
i++;
j++;
}
}
return ans;
}
39 changes: 39 additions & 0 deletions Sliding Window/Fixed/first_negative_number_in_window_of_size_k.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> solve(vector<int> nums, int k)
{
int n = nums.size();
queue<int> qu;
vector<int> ans;
int i = 0;
int j = 0;
while (j < n)
{
if (nums[j] < 0)
{
qu.push(nums[j]);
}
if ((j - i + 1) < k)
{
j++;
}
else if ((j - i + 1) == k)
{
if (qu.size() == 0)
{
ans.push_back(0);
}
else
{
ans.push_back(qu.front());
if (nums[i] == qu.front())
{
qu.pop();
}
}
i++;
j++;
}
}
}
38 changes: 38 additions & 0 deletions Sliding Window/Fixed/max_of_all_subarray_of_size_k.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> solve(vector<int> nums, int k)
{
int n = nums.size();
int i = 0;
int j = 0;
vector<int> ans;
deque<int> list;
if (k < nums.size())
{
ans.push_back(*max_element(nums.begin(), nums.end()));
return ans;
}
while (j < n)
{
while (list.size() > 0 && list.back() < nums[j])
{
list.pop_back();
}
list.push_back(nums[j]);
if ((j - i + 1) < k)
{
j++;
}
else if ((j - i + 1) == k)
{
ans.push_back(list.front());
if (list.front() == nums[i])
{
list.pop_front();
}
i++;
j++;
}
}
}
27 changes: 27 additions & 0 deletions Sliding Window/Fixed/max_sum_subarray_of_size_k.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;

int solve(vector<int> nums, int k)
{
int n = nums.size();
int i = 0;
int j = 0;
int sum = 0;
int mx = INT_MIN;
while (j < n)
{
sum += nums[j];
if ((j - i + 1) < k)
{
j++;
}
else if ((j - i + 1) == k)
{
mx = max(mx, sum);
sum -= nums[i];
i++;
j++;
}
}
return mx;
}
Loading

0 comments on commit e41a63f

Please sign in to comment.