Skip to content

Commit c65d3ac

Browse files
committed
Just get into Agoda.com recruitment process today, after the recruiter found me via Linkedin. Updated Linkedin profile few days back so expecting more surprise knock.
1 parent e855068 commit c65d3ac

5 files changed

+201
-0
lines changed

Diff for: source-code/Beautiful_Arrangement_II.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<int> constructArray(int n, int k) {
4+
vector<int> result;
5+
if(k > n - 1) {
6+
return result;
7+
}
8+
int left = 1, right = n;
9+
for(int i = 0, flip = 0; i < n; i++, flip ^= 1) {
10+
if(k == 1) {
11+
if(flip) {
12+
for(int i = right; i >= left; i--) {
13+
result.push_back(i);
14+
}
15+
} else {
16+
for(int i = left; i <= right; i++) {
17+
result.push_back(i);
18+
}
19+
}
20+
break;
21+
}
22+
if(i > 0) { k--; }
23+
result.push_back(flip ? left++ : right--);
24+
}
25+
return result;
26+
}
27+
};

Diff for: source-code/Find_Duplicate_Subtrees.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
/**
11+
* Definition for a binary tree node.
12+
* struct TreeNode {
13+
* int val;
14+
* TreeNode *left;
15+
* TreeNode *right;
16+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
17+
* };
18+
*/
19+
// AC
20+
// postorder and preorder signature will work, inorder not.
21+
class Solution {
22+
string preorder(TreeNode* root, unordered_map<string, int>& preOrderMap, vector<TreeNode*>& result) {
23+
if(!root) {
24+
return "#";
25+
}
26+
signature += "," + to_string(root->val);
27+
string signature = preorder(root->left, preOrderMap, result);
28+
signature += "," + preorder(root->right, preOrderMap, result);
29+
// signature += "," + to_string(root->val); would work too
30+
31+
if(preOrderMap[signature] == 1) {
32+
result.push_back(root);
33+
}
34+
preOrderMap[signature]++;
35+
return signature;
36+
}
37+
public:
38+
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
39+
vector<TreeNode*> result;
40+
unordered_map<string, int> preOrderMap;
41+
preorder(root, preOrderMap, result);
42+
return result;
43+
}
44+
};
45+
46+
// brute-force, TLE, 121/167 testcases passed
47+
class Solution {
48+
bool isSame(TreeNode* rootA, TreeNode* rootB) {
49+
if(!rootA and !rootB) {
50+
return true;
51+
}
52+
if(!rootA or !rootB) {
53+
return false;
54+
}
55+
return rootA->val == rootB->val and isSame(rootA->left, rootB->left) and isSame(rootA->right, rootB->right);
56+
}
57+
58+
bool hasDuplicate(TreeNode* node, TreeNode* root, unordered_set<TreeNode*>& visited) {
59+
if(!root) {
60+
return false;
61+
}
62+
if(visited.find(root) == visited.end() and isSame(node, root)) {
63+
visited.insert(root);
64+
return true;
65+
}
66+
int leftDuplicate = hasDuplicate(node, root->left, visited);
67+
int rightDuplicate = hasDuplicate(node, root->right, visited);
68+
69+
return leftDuplicate or rightDuplicate;
70+
}
71+
72+
void findDuplicateSubtrees(TreeNode* node, TreeNode* root, vector<TreeNode*>& result, unordered_set<TreeNode*>& visited) {
73+
if(!node) {
74+
return;
75+
}
76+
if(visited.find(node) == visited.end()) {
77+
visited.insert(node);
78+
if(hasDuplicate(node, root, visited)) {
79+
result.push_back(node);
80+
}
81+
}
82+
83+
findDuplicateSubtrees(node->left, root, result, visited);
84+
findDuplicateSubtrees(node->right, root, result, visited);
85+
}
86+
public:
87+
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
88+
unordered_set<TreeNode*> visited;
89+
vector<TreeNode*> result;
90+
findDuplicateSubtrees(root, root, result, visited);
91+
return result;
92+
}
93+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
bool largerEnough(int x, int m, int n, int k) {
3+
int count = 0;
4+
for(int i = 1; i <= m; i++) {
5+
count += min(x / i, n);
6+
}
7+
return count >= k;
8+
}
9+
public:
10+
int findKthNumber(int m, int n, int k) {
11+
int left = 1, right = m * n;
12+
while(left < right) {
13+
int mid = left + (right - left) / 2;
14+
if(!largerEnough(mid, m, n, k)) {
15+
left = mid + 1;
16+
} else {
17+
right = mid;
18+
}
19+
}
20+
return left;
21+
}
22+
};

Diff for: source-code/Non-decreasing_Array.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool checkPossibility(vector<int>& nums) {
4+
int changed = 0;
5+
for(int i = 0; i < nums.size() - 1; i++) {
6+
if(nums[i] > nums[i + 1]) {
7+
if(changed > 0) {
8+
return false;
9+
}
10+
if(i - 1 >= 0) {
11+
if(nums[i + 1] >= nums[i - 1]) {
12+
nums[i] = nums[i - 1];
13+
} else if(nums[i + 1] < nums[i - 1]) {
14+
nums[i + 1] = nums[i];
15+
}
16+
} else {
17+
nums[i] = nums[i + 1];
18+
}
19+
++changed;
20+
}
21+
}
22+
return true;
23+
}
24+
};

Diff for: source-code/Subtree_of_Another_Tree.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,39 @@ class Solution {
1919
if(s == nullptr) return false;
2020
return isSameTree(s, t) or isSubtree(s->left, t) or isSubtree(s->right, t);
2121
}
22+
};
23+
24+
// using preorder hash
25+
class Solution {
26+
string preorderHash(TreeNode* root) {
27+
if(!root) {
28+
return "#";
29+
}
30+
return to_string(root->val)
31+
+ "," + preorderHash(root->left)
32+
+ "," + preorderHash(root->right);
33+
}
34+
35+
string checkSubtree(TreeNode* root, string const& signature, bool& isSubtree) {
36+
if(!root) {
37+
return "#";
38+
}
39+
if(isSubtree) {
40+
return "";
41+
}
42+
string preorder = to_string(root->val);
43+
preorder += "," + checkSubtree(root->left, signature, isSubtree);
44+
preorder += "," + checkSubtree(root->right, signature, isSubtree);
45+
if(preorder == signature) {
46+
isSubtree = true;
47+
}
48+
return preorder;
49+
}
50+
public:
51+
bool isSubtree(TreeNode* s, TreeNode* t) {
52+
string signature = preorderHash(t);
53+
bool isSubtree = false;
54+
checkSubtree(s, signature, isSubtree);
55+
return isSubtree;
56+
}
2257
};

0 commit comments

Comments
 (0)