-
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.
Merge branch 'master' of https://github.com/khushal87/DSA-Essential
- Loading branch information
Showing
8 changed files
with
179 additions
and
91 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
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
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,14 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool isPrime(int n) | ||
{ | ||
for (int i = 2; i * i <= n; i++) | ||
{ | ||
if (n % i == 0) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
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,23 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<pair<int, int>> primeFac(int n) | ||
{ | ||
vector<pair<int, int>> v; | ||
for (int i = 2; i * i <= n; i++) | ||
{ | ||
if (n % i == 0) | ||
{ | ||
int ctr = 0; | ||
while (n % i == 0) | ||
{ | ||
n /= i; | ||
ctr++; | ||
} | ||
v.push_back({i, ctr}); | ||
} | ||
} | ||
if (n > 1) | ||
v.push_back({n, 1}); | ||
return v; | ||
} |
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,29 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// n log log n | ||
vector<int> primeNumbers(int n) | ||
{ | ||
vector<int> ans; | ||
vector<int> sieve(n, true); | ||
sieve[0]=false; | ||
sieve[1]= false; | ||
for (int i = 2; i * i <= n; i++) | ||
{ | ||
if (sieve[i]) | ||
{ | ||
for (int j = i * i; j <= n; j += i) | ||
{ | ||
sieve[j] = false; | ||
} | ||
} | ||
} | ||
for (auto j : sieve) | ||
{ | ||
if (j == true) | ||
{ | ||
ans.push_back(j); | ||
} | ||
} | ||
return ans; | ||
} |
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
Node(int x) | ||
{ | ||
data = x; | ||
left = nullptr; | ||
right = nullptr; | ||
} | ||
}; | ||
|
||
int search(vector<int> in, int start, int end, int x) | ||
{ | ||
int idx = 0; | ||
for (int i = start; i <= end; i++) | ||
{ | ||
if (in[i] == x) | ||
{ | ||
idx = i; | ||
} | ||
} | ||
return idx; | ||
} | ||
|
||
int preIdx = 0; | ||
Node *solve(vector<int> &pre, vector<int> &in, int start, int end) | ||
{ | ||
|
||
if (start > end) | ||
{ | ||
return nullptr; | ||
} | ||
Node *tNode = new Node(pre[preIdx++]); | ||
if (start == end) | ||
{ | ||
return tNode; | ||
} | ||
int idx = search(in, start, end, tNode->data); | ||
tNode->left = solve(pre, in, start, idx - 1); | ||
tNode->right = solve(pre, in, idx + 1, end); | ||
return tNode; | ||
} | ||
|
||
Node *buildTree(vector<int> &pre, vector<int> &in) | ||
{ | ||
int start = 0; | ||
int end = in.size() - 1; | ||
return solve(pre, in, start, end); | ||
} |
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 |
---|---|---|
@@ -1,75 +1,87 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Trie | ||
class TrieNode | ||
{ | ||
public: | ||
Trie() | ||
bool end; | ||
TrieNode *children[26]; | ||
int cnt; | ||
TrieNode() | ||
{ | ||
root = getNode('/' - 'a'); | ||
end = false; | ||
cnt = 0; | ||
for (int i = 0; i < 26; i++) | ||
{ | ||
children[i] = NULL; | ||
} | ||
} | ||
//size of a node is 217 bytes | ||
struct TrieNode | ||
{ | ||
char val; | ||
int count, endsHere; | ||
TrieNode *child[26]; | ||
}; | ||
}; | ||
|
||
class Trie | ||
{ | ||
public: | ||
/** Initialize your data structure here. */ | ||
TrieNode *root; | ||
|
||
TrieNode *getNode(int idx) | ||
Trie() | ||
{ | ||
TrieNode *newnode = new TrieNode; | ||
newnode->val = 'a' + idx; | ||
newnode->count = newnode->endsHere = 0; | ||
for (int i = 0; i < 26; i++) | ||
{ | ||
newnode->child[i] = NULL; | ||
} | ||
return newnode; | ||
root = new TrieNode(); | ||
} | ||
|
||
/** Inserts a word into the trie. */ | ||
void insert(string word) | ||
{ | ||
TrieNode *curr = root; | ||
int idx; | ||
TrieNode *node = this->root; | ||
for (auto j : word) | ||
{ | ||
idx = j - '0'; | ||
if (curr->child[idx] == NULL) | ||
curr->child[idx] = getNode(idx); | ||
curr->child[idx]->count += 1; | ||
curr = curr->child[idx]; | ||
if (!node->children[j - 'a']) | ||
{ | ||
node->children[j - 'a'] = new TrieNode(); | ||
} | ||
node = node->children[j - 'a']; | ||
node->cnt++; | ||
} | ||
curr->endsHere += 1; | ||
node->end = true; | ||
} | ||
|
||
/** Returns if the word is in the trie. */ | ||
bool search(string word) | ||
{ | ||
TrieNode *curr = root; | ||
int idx; | ||
TrieNode *node = root; | ||
for (auto j : word) | ||
{ | ||
idx = j - 'a'; | ||
if (curr->child[idx] == NULL) | ||
if (!node->children[j - 'a']) | ||
{ | ||
return false; | ||
curr = curr->child[idx]; | ||
} | ||
node = node->children[j - 'a']; | ||
} | ||
return curr->endsHere > 0; | ||
return node->end; | ||
} | ||
|
||
/** Returns if there is any word in the trie that starts with the given prefix. */ | ||
bool startsWith(string prefix) | ||
{ | ||
TrieNode *curr = root; | ||
int idx; | ||
TrieNode *node = root; | ||
for (auto j : prefix) | ||
{ | ||
idx = j - 'a'; | ||
if (curr->child[idx] == NULL) | ||
if (!node->children[j - 'a']) | ||
{ | ||
return false; | ||
curr = curr->child[idx]; | ||
} | ||
node = node->children[j - 'a']; | ||
} | ||
return curr->count > 0; | ||
if (node->cnt > 0) | ||
return true; | ||
else | ||
return false; | ||
} | ||
}; | ||
}; | ||
|
||
/** | ||
* Your Trie object will be instantiated and called as such: | ||
* Trie* obj = new Trie(); | ||
* obj->insert(word); | ||
* bool param_2 = obj->search(word); | ||
* bool param_3 = obj->startsWith(prefix); | ||
*/ |