-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
289 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
Array/minimum_number_platforms_required_railwaybus_station.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
String/length_of_substring_without_repeating_character.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |