Skip to content

Commit 1ab0cfa

Browse files
committed
554/656 problems solved. Facebook Data Engineer phone interview passed. Waiting for possible onsite interview invitation.
1 parent 8de4f1c commit 1ab0cfa

8 files changed

+858
-450
lines changed

Diff for: README.md

+457-450
Large diffs are not rendered by default.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findLengthOfLCIS(vector<int>& nums) {
4+
int maxLen = 0;
5+
int i = 0;
6+
while(i < nums.size()) {
7+
int currLen = 0;
8+
int prev = nums[i++];
9+
currLen++;
10+
while(i < nums.size() and nums[i] > prev) {
11+
prev = nums[i++];
12+
currLen++;
13+
}
14+
maxLen = max(maxLen, currLen);
15+
}
16+
return maxLen;
17+
}
18+
};

Diff for: source-code/Map_Sum_Pairs.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class MapSum {
2+
class Trie {
3+
class trieNode {
4+
public:
5+
int value;
6+
int sum;
7+
unordered_map<char, trieNode*> children;
8+
trieNode(): value(0), sum(0) {
9+
children = unordered_map<char, trieNode*>();
10+
}
11+
trieNode(int value): value(value), sum(value) {
12+
children = unordered_map<char, trieNode*>();
13+
}
14+
};
15+
16+
int insert(int indx, string const& key, const int value, trieNode* pCrawl) {
17+
if(indx == key.length() - 1) {
18+
if(pCrawl->children.find(key[indx]) == pCrawl->children.end()) {
19+
pCrawl->children[key[indx]] = new trieNode(value);
20+
return value;
21+
}
22+
int oldValue = pCrawl->children[key[indx]]->value;
23+
pCrawl->children[key[indx]]->value = value;
24+
pCrawl->children[key[indx]]->sum += value;
25+
pCrawl->children[key[indx]]->sum -= oldValue;
26+
return value - oldValue;
27+
}
28+
if(pCrawl->children.find(key[indx]) == pCrawl->children.end()) {
29+
pCrawl->children[key[indx]] = new trieNode();
30+
}
31+
int deltaSum = insert(indx + 1, key, value, pCrawl->children[key[indx]]);
32+
33+
pCrawl->children[key[indx]]->sum += deltaSum;
34+
35+
return deltaSum;
36+
}
37+
38+
trieNode* root;
39+
public:
40+
Trie(): root(new trieNode()) {}
41+
42+
int query(string const& key) {
43+
trieNode* pCrawl = root;
44+
for(int i = 0; i < key.length(); i++) {
45+
if(pCrawl->children.find(key[i]) == pCrawl->children.end()) {
46+
return 0;
47+
}
48+
pCrawl = pCrawl->children[key[i]];
49+
}
50+
return pCrawl->sum;
51+
}
52+
53+
void insert(string const& key, int const value) {
54+
insert(0, key, value, root);
55+
}
56+
};
57+
Trie* trie;
58+
public:
59+
/** Initialize your data structure here. */
60+
MapSum() {
61+
trie = new Trie();
62+
}
63+
64+
void insert(string key, int val) {
65+
trie->insert(key, val);
66+
}
67+
68+
int sum(string prefix) {
69+
return trie->query(prefix);
70+
}
71+
};
72+
73+
/**
74+
* Your MapSum object will be instantiated and called as such:
75+
* MapSum obj = new MapSum();
76+
* obj.insert(key,val);
77+
* int param_2 = obj.sum(prefix);
78+
*/

Diff for: source-code/Reverse_Pairs.cpp

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// AC
2+
class Solution {
3+
int countReversePair(vector<int>& A, int start, int mid, int end) {
4+
int j = mid + 1;
5+
int count = 0;
6+
for (int i = start; i <= mid; i++) {
7+
while (j <= end and A[i] > 2LL * A[j]) {
8+
j++;
9+
}
10+
count += j - (mid + 1);
11+
}
12+
return count;
13+
}
14+
15+
void merge(vector<int>& A, int start, int mid, int end) {
16+
int n1 = (mid - start + 1);
17+
int n2 = (end - mid);
18+
int L[n1], R[n2];
19+
20+
for (int i = 0; i < n1; i++) {
21+
L[i] = A[start + i];
22+
}
23+
for (int j = 0; j < n2; j++) {
24+
R[j] = A[mid + 1 + j];
25+
}
26+
int i = 0, j = 0, k = start;
27+
while(i < n1 and j < n2) {
28+
if(L[i] < R[j]) {
29+
A[k++] = L[i++];
30+
} else {
31+
A[k++] = R[j++];
32+
}
33+
}
34+
while(i < n1) {
35+
A[k++] = L[i++];
36+
}
37+
while(j < n2) {
38+
A[k++] = R[j++];
39+
}
40+
}
41+
42+
int mergesortAndCount(vector<int>& A, int start, int end) {
43+
if (start > end) {
44+
return 0;
45+
}
46+
int mid = start + (end - start) / 2;
47+
int count = mergesortAndCount(A, start, mid) + mergesortAndCount(A, mid + 1, end);
48+
count += countReversePair(A, start, mid, end);
49+
merge(A, start, mid, end);
50+
51+
return count;
52+
}
53+
54+
public:
55+
int reversePairs(vector<int>& nums) {
56+
return mergesortAndCount(nums, 0, nums.size() - 1);
57+
}
58+
59+
};
60+
61+
// TLE (for skewed binary tree)
62+
class Solution {
63+
class BST {
64+
public:
65+
class BSTNode {
66+
public:
67+
BSTNode* left;
68+
BSTNode* right;
69+
int value;
70+
int largerElemCount;
71+
BSTNode(int value) {
72+
this->value = value;
73+
left = right = nullptr;
74+
largerElemCount = 1;
75+
}
76+
};
77+
BST(): root(nullptr) {}
78+
79+
int search(long long const key, BSTNode* curr) {
80+
if(!curr) {
81+
return 0;
82+
}
83+
if(curr->value == key) {
84+
return curr->largerElemCount;
85+
}
86+
if(curr->value > key) {
87+
return curr->largerElemCount + search(key, curr->left);
88+
} else {
89+
return search(key, curr->right);
90+
}
91+
}
92+
93+
BSTNode* insert(int const value, BSTNode* curr) {
94+
if(!curr) {
95+
return new BSTNode(value);
96+
}
97+
if(curr->value == value) {
98+
curr->largerElemCount++;
99+
return curr;
100+
}
101+
if(curr->value > value) {
102+
curr->left = insert(value, curr->left);
103+
}else {
104+
curr->largerElemCount++;
105+
curr->right = insert(value, curr->right);
106+
}
107+
return curr;
108+
}
109+
110+
BSTNode* root;
111+
};
112+
113+
public:
114+
int reversePairs(vector<int>& nums) {
115+
int result = 0;
116+
BST* bst = new BST();
117+
for(int i = 0; i < nums.size(); i++) {
118+
result += bst->search(2LL * nums[i] + 1LL, bst->root);
119+
bst->root = bst->insert(nums[i], bst->root);
120+
}
121+
return result;
122+
}
123+
};

Diff for: source-code/Second_Minimum Node_In_a_Binary_Tree.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
void findSecondMinimumValue(TreeNode* root, const int minimum, int& secondMinimum) {
12+
if(!root) return;
13+
14+
if(root->val > minimum) {
15+
secondMinimum = (secondMinimum != -1) ? min(secondMinimum, root->val) : root->val;
16+
}
17+
18+
if(root->right != nullptr and root->right->val > root->val) {
19+
secondMinimum = (secondMinimum != -1) ? min(secondMinimum, root->right->val) : root->right->val;
20+
} else {
21+
findSecondMinimumValue(root->right, minimum, secondMinimum);
22+
}
23+
24+
findSecondMinimumValue(root->left, minimum, secondMinimum);
25+
}
26+
public:
27+
int findSecondMinimumValue(TreeNode* root) {
28+
int secondMinimum = -1;
29+
findSecondMinimumValue(root, root->val, secondMinimum);
30+
return secondMinimum;
31+
}
32+
};

Diff for: source-code/Subtree_of_Another_Tree.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
bool isSameTree(TreeNode* s, TreeNode* t) {
12+
if(!s and !t) return true;
13+
if(!s or !t) return false;
14+
return s->val == t->val and isSameTree(s->left, t->left) and isSameTree(s->right, t->right);
15+
}
16+
17+
public:
18+
bool isSubtree(TreeNode* s, TreeNode* t) {
19+
if(s == nullptr) return false;
20+
return isSameTree(s, t) or isSubtree(s->left, t) or isSubtree(s->right, t);
21+
}
22+
};

Diff for: source-code/Valid_Palindrome_II.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
bool isPalindrome(string s, int left, int right) {
3+
while(left < right) {
4+
if(s[left] != s[right]) {
5+
return false;
6+
}
7+
++left, --right;
8+
}
9+
return true;
10+
}
11+
public:
12+
bool validPalindrome(string s) {
13+
int left = 0, right = s.length() - 1;
14+
while(left <= right and s[left] == s[right]) {
15+
++left, --right;
16+
}
17+
if(left >= right) {
18+
return true;
19+
}
20+
return isPalindrome(s, left + 1, right) or isPalindrome(s, left, right - 1);
21+
}
22+
};

0 commit comments

Comments
 (0)