Skip to content

Commit

Permalink
add khushal87 new code
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Mar 4, 2021
1 parent 9e3e054 commit baee519
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 28 deletions.
55 changes: 55 additions & 0 deletions Array/3sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> threeSum(vector<int> &nums, int target)
{
sort(nums.begin(), nums.end());
int l, r;
int n = nums.size();
set<vector<int>> ans;
for (int i = 0; i < n - 2; i++)
{
l = i + 1;
r = n - 1;
while (l < r)
{
if ((nums[l] + nums[r] + nums[i]) == 0)
{
vector<int> temp = {nums[i], nums[l], nums[r]};
ans.insert(temp);
l++;
}
else if ((nums[l] + nums[r] + nums[i]) < 0)
{
l++;
}
else
{
r--;
}
}
}
vector<vector<int>> fin;
for (auto j : ans)
fin.push_back(j);
return fin;
}

vector<vector<int>> method2(vector<int> nums, int target)
{
int sz = nums.size();
vector<vector<int>> ans;
for (int i = 0; i < sz - 2; i++)
{
unordered_set<int> st;
int curr = target - nums[i];
for (int j = i + 1; j < sz; j++)
{
if (st.find(curr - nums[j]) != st.end())
{
ans.push_back({nums[i], nums[j], curr - nums[j]});
}
st.insert(nums[j]);
}
}
}
26 changes: 26 additions & 0 deletions Array/minimum_number_platforms_required_railwaybus_station.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;

int findPlatform(int arr[], int dep[], int n)
{
sort(arr, arr + n);
sort(dep, dep + n);
int i = 1, j = 0;
int count = 1, mx = 1;
while (i < n && j < n)
{
if (arr[i] <= dep[j])
{
count++;
i++;
}
else if (arr[i] > dep[j])
{
count--;
j++;
}
if (count > mx)
mx = count;
}
return mx;
}
77 changes: 77 additions & 0 deletions Grid/rotten_orange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <bits/stdc++.h>
using namespace std;

bool isVaid(int i, int j, int r, int c)
{
if (i >= 0 && i < r && j >= 0 && j < c)
return true;
else
return false;
}

int x[4] = {0, 0, 1, -1};
int y[4] = {1, -1, 0, 0};

int orangesRotting(vector<vector<int>> &arr)
{
int r = arr.size();
int c = arr[0].size();
queue<pair<int, int>> qu;
int ones = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (arr[i][j] == 2)
{
qu.push({i, j});
}
if (arr[i][j] == 1)
ones++;
}
}
if (qu.empty())
{
if (ones == 0)
return 0;
else
return -1;
}
int cnt = -1;
while (!qu.empty())
{
cnt++;
int sz = qu.size();
for (int i = 0; i < sz; i++)
{
pair<int, int> tmp = qu.front();
qu.pop();
for (int i = 0; i < 4; i++)
{
int x_dir = tmp.first + x[i];
int y_dir = tmp.second + y[i];
if (isVaid(x_dir, y_dir, r, c) && arr[x_dir][y_dir] == 1)
{
arr[x_dir][y_dir] = 2;
qu.push({x_dir, y_dir});
}
}
}
}
int flag = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (arr[i][j] == 1)
{
flag = 1;
break;
}
}
}
if (flag == 1)
return -1;
else
return cnt;
}
30 changes: 30 additions & 0 deletions Queue/first_non_repeating_character_in_stream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

void findFirstNonRepeating(string s)
{
int sz = s.length();
queue<char> qu;
vector<int> count(26, 0);
for (int i = 0; i < sz; i++)
{
qu.push(s[i]);
count[s[i] - 'a']++;
while (!qu.empty())
{
if (count[qu.front() - 'a'] > 1)
{
qu.pop();
}
else
{
cout << qu.front() << " ";
break;
}
}
if (qu.empty())
{
cout << -1 << endl;
}
}
}
40 changes: 12 additions & 28 deletions Stack/balanced_paranthesis.c++
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bits/stdc++.h>
using namespace std;

void solveBalancedPar(string s)
bool solve(string s)
{
stack<char> st;
for (auto j : s)
Expand All @@ -15,36 +15,20 @@ void solveBalancedPar(string s)
if (st.empty())
{
st.push(j);
continue;
}
char x = st.top();
if ((j == ']' && x == '[') || (j == '}' && x == '{') || (j == ')' && x == '('))
else
{
st.pop();
continue;
char x = st.top();
if ((j == ']' && x == '[') || (j == '}' && x == '{') || (j == ')' && x == '('))
{
st.pop();
}
else
{
st.push(j);
}
}
st.push(j);
}
}
if (st.size() > 0)
{
cout << "not balanced" << endl;
}
else
{
cout << "balanced" << endl;
}
}

int main()
{
int t;
cin >> t;
while (t--)
{
string s;
cin >> s;
solveBalancedPar(s);
}
return 0;
return st.empty();
}
24 changes: 24 additions & 0 deletions String/length_of_substring_without_repeating_character.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;

int longestUnique(string s)
{
int n = s.length();
int res = 0;
for (int i = 0; i < n; i++)
{
vector<bool> vis(256);
for (int j = i; j < n; j++)
{
if (vis[s[j]] == true)
break;
else
{
res = max(res, j - i + 1);
vis[s[j]] = true;
}
}
vis[s[i]] = false;
}
return res;
}
35 changes: 35 additions & 0 deletions Tree/is_inverted_subtree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;

struct Tree
{
int val;
Tree *left;
Tree *right;
};

bool isIdentical(Tree *root1, Tree *root2)
{
if (root1 == nullptr && root2 == nullptr)
return true;
if (root1 == nullptr || root2 == nullptr)
return false;
if (root1->val != root2->val)
return false;
bool op1 = isIdentical(root1->left, root2->left) &&
isIdentical(root1->right, root2->right);
bool op2 = isIdentical(root1->left, root2->right) &&
isIdentical(root1->right, root2->left);
return op1 || op2;
}

bool solve(Tree *source, Tree *target)
{
if (target == nullptr)
return false;
if (source == nullptr)
return true;
if (isIdentical(target, source))
return true;
return solve(source, target->left) || solve(source, target->right);
}
30 changes: 30 additions & 0 deletions Tree/sum_of_nodes_at_max_level.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

struct Tree
{
int data;
Tree *left;
Tree *rigth;
};

int maxLevel = INT_MIN, sum = 0;
void sumOfNodes(Tree *root, int level)
{
if (root == nullptr)
return;
else
{
if (level > maxLevel)
{
maxLevel = level;
sum = root->data;
}
if (level == maxLevel)
{
sum += root->data;
}
sumOfNodes(root->left, level + 1);
sumOfNodes(root->rigth, level + 1);
}
}

0 comments on commit baee519

Please sign in to comment.