Skip to content

Commit 23ba283

Browse files
author
kaidul
committed
Bloomberg onsite ahead. Solved all problems asked in Bloomberg
1 parent 5500544 commit 23ba283

13 files changed

+895
-596
lines changed

README.md

+593-582
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
vector<TreeNode*> allPossibleFBT(int n, vector<TreeNode*>& result) {
12+
if(n == 0) {
13+
result.push_back(nullptr);
14+
return result;
15+
}
16+
if(n % 2 == 0) {
17+
return vector<TreeNode*>();
18+
}
19+
for(int i = 1; i <= n; i++) {
20+
int leftTreeNodeCount = i - 1;
21+
int rightTreeNodeCount = n - i;
22+
23+
vector<TreeNode*> leftSubTrees;
24+
allPossibleFBT(leftTreeNodeCount, leftSubTrees);
25+
26+
vector<TreeNode*> rightSubTrees;
27+
allPossibleFBT(rightTreeNodeCount, rightSubTrees);
28+
29+
for(TreeNode* leftSubTree : leftSubTrees) {
30+
for(TreeNode* rightSubTree : rightSubTrees) {
31+
TreeNode* root = new TreeNode(0);
32+
root->left = leftSubTree;
33+
root->right = rightSubTree;
34+
35+
result.push_back(root);
36+
}
37+
}
38+
39+
}
40+
return result;
41+
}
42+
43+
public:
44+
vector<TreeNode*> allPossibleFBT(int N) {
45+
vector<TreeNode*> result;
46+
allPossibleFBT(N, result);
47+
return result;
48+
}
49+
};

source-code/Daily_Temperatures.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<int> dailyTemperatures(vector<int>& temperatures) {
4+
int n = temperatures.size();
5+
vector<int> result(n);
6+
stack<int> indexStack;
7+
indexStack.push(0);
8+
for(int i = 1; i < n; i++) {
9+
if(temperatures[i] > temperatures[i - 1]) {
10+
while(!indexStack.empty() and temperatures[indexStack.top()] < temperatures[i]) {
11+
int indx = indexStack.top();
12+
result[indx] = i - indx;
13+
indexStack.pop();
14+
}
15+
}
16+
indexStack.push(i);
17+
}
18+
while(!indexStack.empty()) {
19+
int indx = indexStack.top();
20+
result[indx] = 0;
21+
indexStack.pop();
22+
}
23+
24+
return result;
25+
}
26+
};

source-code/Find_Pivot_Index.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int pivotIndex(vector<int>& nums) {
4+
if(nums.empty())
5+
return -1;
6+
7+
int n = (int) nums.size();
8+
vector<int> suffixSum(n + 1, 0);
9+
suffixSum[n - 1] = nums[n - 1];
10+
for(int i = n - 2; i >= 0; i--) {
11+
suffixSum[i] = nums[i] + suffixSum[i + 1];
12+
}
13+
int prefixSum = 0;
14+
for(int i = 0; i < n; i++) {
15+
if(prefixSum == suffixSum[i + 1]) {
16+
return i;
17+
}
18+
prefixSum += nums[i];
19+
}
20+
21+
return -1;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
bool isSpecialEquivalent(string const& s, string const& t) {
3+
int freqOdd[26] = {0}, freqEven[26] = {0};
4+
for(int i = 0; i < s.length(); i++) {
5+
if(i & 1) {
6+
freqEven[s[i] - 'a']++;
7+
freqEven[t[i] - 'a']--;
8+
} else {
9+
freqOdd[s[i] - 'a']++;
10+
freqOdd[t[i] - 'a']--;
11+
}
12+
}
13+
for(int i = 0; i < 26; i++) {
14+
if(freqEven[i] < 0 or freqOdd[i] < 0) return false;
15+
}
16+
return true;
17+
}
18+
public:
19+
int numSpecialEquivGroups(vector<string>& A) {
20+
int groupCount = 0;
21+
vector<bool> isGrouped(A.size(), false);
22+
for(int i = 0; i < (int)A.size(); i++) {
23+
if(isGrouped[i]) continue;
24+
groupCount++;
25+
for(int k = i + 1; k < (int)A.size(); k++) {
26+
if(isSpecialEquivalent(A[i], A[k])) {
27+
isGrouped[k] = true;
28+
}
29+
}
30+
}
31+
return groupCount;
32+
}
33+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 increasingBSTRecur(TreeNode* root, TreeNode*& parent, TreeNode*& newRoot) {
12+
if(!root) return;
13+
14+
increasingBSTRecur(root->left, parent, newRoot);
15+
16+
if(!newRoot) {
17+
newRoot = root;
18+
}
19+
root->left = nullptr;
20+
if(parent) parent->right = root;
21+
parent = root;
22+
23+
increasingBSTRecur(root->right, parent, newRoot);
24+
}
25+
public:
26+
TreeNode* increasingBST(TreeNode* root) {
27+
TreeNode* newRoot = nullptr;
28+
TreeNode* parent = nullptr;
29+
increasingBSTRecur(root, parent, newRoot);
30+
31+
return newRoot;
32+
}
33+
};

source-code/Monotonic_Array.cpp

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

source-code/Rectangle_Overlap.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
4+
int x11 = rec1[0], y11 = rec1[1];
5+
int x12 = rec1[2], y12 = rec1[3];
6+
7+
int x21 = rec2[0], y21 = rec2[1];
8+
int x22 = rec2[2], y22 = rec2[3];
9+
10+
return x11 < x22 and x21 < x12 and y11 < y22 and y21 < y12;
11+
}
12+
};

source-code/Rotate_String.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool rotateString(string A, string B) {
4+
if(A.length() != B.length())
5+
return false;
6+
if(A.empty())
7+
return true;
8+
int n = A.length();
9+
for(int i = 0; i < n; i++) {
10+
bool flag = true;
11+
for(int k = 0, j = i; k < n and flag; j++, k++) {
12+
int indx = j % n;
13+
if(A[k] != B[indx]) {
14+
flag = false;
15+
}
16+
}
17+
if(flag) return true;
18+
}
19+
return false;
20+
}
21+
};

source-code/Same_Tree.cpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,9 @@
99
*/
1010
class Solution {
1111
public:
12-
bool isSameTree(TreeNode *p, TreeNode *q) {
12+
bool isSameTree(TreeNode* p, TreeNode* q) {
1313
if(!p and !q) return true;
1414
if((!p and q) or (p and !q)) return false;
15-
/*
16-
if((p and !p->left and !p->right) and (q and !q->left and !q->right))
17-
if(p->val == q->val)
18-
return true;
19-
else
20-
return false;
21-
if(p->val == q->val) {
22-
return isSameTree(p->left, q->left) and isSameTree(p->right, q->right);
23-
} else
24-
return false;
25-
*/
26-
if(p->val != q->val) return false;
27-
return isSameTree(p->left, q->left) and isSameTree(p->right, q->right);
15+
return p->val == q->val and isSameTree(p->left, q->left) and isSameTree(p->right, q->right);
2816
}
2917
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<int> shortestToChar(string S, char C) {
4+
int latestPositionOfC = INT_MAX;
5+
vector<int> result(S.length(), INT_MAX);
6+
for(int i = 0; i < S.length(); i++) {
7+
if(S[i] == C) {
8+
latestPositionOfC = i;
9+
}
10+
if(latestPositionOfC != INT_MAX) {
11+
result[i] = i - latestPositionOfC;
12+
}
13+
}
14+
latestPositionOfC = INT_MAX;
15+
for(int i = S.length() - 1; i >= 0; i--) {
16+
if(S[i] == C) {
17+
latestPositionOfC = i;
18+
}
19+
if(latestPositionOfC != INT_MAX) {
20+
result[i] = min(result[i], latestPositionOfC - i);
21+
}
22+
}
23+
24+
return result;
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
4+
if(nums.empty())
5+
return 0;
6+
int result = 0;
7+
int product = 1;
8+
int left = 0, right = 0;
9+
while(right < nums.size()) {
10+
product *= nums[right];
11+
while(left <= right and product >= k) {
12+
product /= nums[left++];
13+
}
14+
result += (right - left + 1);
15+
right++;
16+
}
17+
18+
return result;
19+
}
20+
};

source-code/Subdomain_Visit_Count.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<string> subdomainVisits(vector<string>& cpdomains) {
4+
unordered_map<string, int> visitFreq;
5+
for(string cpDomain : cpdomains) {
6+
int spacePos = cpDomain.find(" ");
7+
int noOfVisit = atoi(cpDomain.substr(0, spacePos).c_str());
8+
string domain = cpDomain.substr(spacePos + 1);
9+
vector<string> subDomains;
10+
while(true) {
11+
subDomains.push_back(domain);
12+
int dotPos = domain.find(".");
13+
if(dotPos == string::npos) {
14+
dotPos = domain.length();
15+
break;
16+
}
17+
domain = domain.substr(dotPos + 1);
18+
}
19+
for(string subDomain : subDomains) {
20+
visitFreq[subDomain] += noOfVisit;
21+
}
22+
}
23+
vector<string> result;
24+
for(auto entry : visitFreq) {
25+
result.push_back(to_string(entry.second) + " " + entry.first);
26+
}
27+
28+
return result;
29+
}
30+
};

0 commit comments

Comments
 (0)