Skip to content

Commit f93104d

Browse files
committed
532/602 solved. Progressed to Amazon web services SDE onsite-interview stage
1 parent b0c508f commit f93104d

9 files changed

+229
-3
lines changed

source-code/Add_One_Row_to_Tree.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 addOneRow(TreeNode* curr, int depth, const int v, const int d) {
12+
if(depth == d - 1) {
13+
TreeNode* left = curr->left;
14+
curr->left = new TreeNode(v);
15+
curr->left->left = left;
16+
TreeNode* right = curr->right;
17+
curr->right = new TreeNode(v);
18+
curr->right->right = right;
19+
return;
20+
}
21+
if(curr->left) addOneRow(curr->left, depth + 1, v, d);
22+
if(curr->right) addOneRow(curr->right, depth + 1, v, d);
23+
}
24+
public:
25+
TreeNode* addOneRow(TreeNode* root, int v, int d) {
26+
if(d == 1) {
27+
TreeNode* newRoot = new TreeNode(v);
28+
newRoot->left = root;
29+
return newRoot;
30+
}
31+
addOneRow(root, 1, v, d);
32+
return root;
33+
}
34+
};
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
int countNumbers(vector<int> const& nums, int mid) {
3+
int cnt = 0;
4+
for(int i = 0; i < nums.size(); ++i) {
5+
cnt += (nums[i] <= mid);
6+
}
7+
return cnt;
8+
}
9+
public:
10+
int findDuplicate(vector<int>& nums) {
11+
if(nums.empty()) return 0;
12+
int left = 0, right = nums.size() - 1;
13+
while(left < right) {
14+
int mid = left + (right - left) / 2;
15+
int cnt = countNumbers(nums, mid);
16+
if(cnt <= mid) {
17+
left = mid + 1;
18+
} else {
19+
right = mid;
20+
}
21+
}
22+
return left;
23+
}
24+
};
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int maxDistance(vector<vector<int>>& arrays) {
4+
int left = arrays[0][0], right = arrays[0].back();
5+
int ans = 0;
6+
for(int i = 1; i < (int)arrays.size(); i++) {
7+
ans = max(ans, max(abs(arrays[i][0] - right), abs(arrays[i].back() - left)));
8+
left = min(left, arrays[i][0]);
9+
right = max(right, arrays[i].back());
10+
}
11+
return ans;
12+
}
13+
};
+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+
public:
12+
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
13+
if(!t1 and !t2) {
14+
return nullptr;
15+
}
16+
TreeNode* currNode = new TreeNode((t1 ? t1->val : 0) + (t2 ? t2->val : 0));
17+
currNode->left = mergeTrees((t1 ? t1->left : nullptr), (t2 ? t2->left : nullptr));
18+
currNode->right = mergeTrees((t1 ? t1->right : nullptr), (t2 ? t2->right : nullptr));
19+
20+
return currNode;
21+
}
22+
};

source-code/Minimum_Factorization.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int smallestFactorization(int a) {
4+
if(a < 10) { return a; }
5+
int factors[40];
6+
int i = 0;
7+
for(int d = 9; d > 1; d--) {
8+
while(a % d == 0) {
9+
a /= d;
10+
factors[i++] = d;
11+
}
12+
}
13+
if(a > 10) {
14+
return 0;
15+
}
16+
long long result = 0LL;
17+
for(int k = i - 1; k >= 0; k--) {
18+
result = result * 10 + factors[k];
19+
if(result > INT_MAX) { return 0; }
20+
}
21+
return (int)result;
22+
}
23+
};

source-code/Minimum_Number_of_Arrows_to_Burst_Balloons.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ class Solution {
44
if(points.empty()) return 0;
55
sort(points.begin(), points.end());
66
int n = points.size();
7-
int balloons = 0;
7+
int arrows = 0;
88
for(int i = 0; i < n; ++i) {
99
int boundary = points[i].second;
1010
while(i + 1 < n and points[i + 1].first <= boundary) {
1111
boundary = min(boundary, points[i + 1].second);
1212
++i;
1313
}
14-
balloons++;
14+
arrows++;
1515
}
1616

17-
return balloons;
17+
return arrows;
1818
}
1919
};

source-code/Task_Scheduler.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// round robin
2+
class Solution {
3+
public:
4+
int leastInterval(vector<char>& tasks, int n) {
5+
unordered_map<char, int> taskFreq;
6+
for (char task : tasks) {
7+
taskFreq[task]++;
8+
}
9+
priority_queue<pair<int, char>> pQ;
10+
for (pair<char, int> taskCount : taskFreq) {
11+
pQ.push({taskCount.second, taskCount.first});
12+
}
13+
int interval = 0;
14+
int cycle = n + 1;
15+
while(!pQ.empty()) {
16+
int tasks = 0;
17+
vector<pair<int, char>> tmp;
18+
for(int i = 0; i < cycle; i++) {
19+
if(!pQ.empty()) {
20+
tmp.push_back(pQ.top());
21+
pQ.pop();
22+
tasks++;
23+
}
24+
}
25+
for(auto task : tmp) {
26+
if(--task.first) {
27+
pQ.push(task);
28+
}
29+
}
30+
interval += !pQ.empty() ? cycle : tasks;
31+
}
32+
return interval;
33+
34+
}
35+
};

source-code/Valid_Triangle_Number.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int triangleNumber(vector<int>& nums) {
4+
int n = (int)nums.size();
5+
if(n <= 2) return 0;
6+
int count = 0;
7+
sort(nums.begin(), nums.end());
8+
int offset = 0;
9+
while(offset < n and nums[offset] == 0) { offset++; }
10+
for(int i = n - 1; i >= offset + 2; i--) {
11+
int a = nums[i];
12+
for(int j = i - 1; j >= offset + 1; j--) {
13+
int b = nums[j];
14+
int left = offset, right = j - 1;
15+
int cIndx = -1;
16+
while(left <= right) {
17+
int mid = left + (right - left) / 2;
18+
if(nums[mid] + b > a) {
19+
cIndx = mid;
20+
right = mid - 1;
21+
} else if(nums[mid] + b <= a) {
22+
left = mid + 1;
23+
}
24+
}
25+
if(cIndx != -1) {
26+
count += (j - cIndx);
27+
}
28+
}
29+
}
30+
return count;
31+
}
32+
};

source-code/Wiggle_Subsequence.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// O(n^2) dynamic programming
12
class Solution {
23
public:
34
int wiggleMaxLength(vector<int>& nums) {
@@ -19,4 +20,46 @@ class Solution {
1920

2021
return maxLength;
2122
}
23+
};
24+
25+
// O(n) dp
26+
class Solution {
27+
public:
28+
int wiggleMaxLength(vector<int>& nums) {
29+
int n = (int)nums.size();
30+
if(n == 0) return n;
31+
vector<int> down(n, 0), up(n, 0);
32+
down[0] = up[0] = 1;
33+
for(int i = 1; i < n; ++i) {
34+
if(nums[i] > nums[i - 1]) {
35+
up[i] = down[i - 1] + 1;
36+
down[i] = down[i - 1];
37+
} else if(nums[i] < nums[i - 1]) {
38+
up[i] = up[i - 1];
39+
down[i] = up[i - 1] + 1;
40+
} else {
41+
up[i] = up[i - 1];
42+
down[i] = down[i - 1];
43+
}
44+
}
45+
return max(up[n - 1], down[n - 1]);
46+
}
47+
};
48+
49+
// O(n) greedy
50+
class Solution {
51+
public:
52+
int wiggleMaxLength(vector<int>& nums) {
53+
int n = (int)nums.size();
54+
if(n == 0) return 0;
55+
int up = 1, down = 1;
56+
for(int i = 1; i < n; ++i) {
57+
if(nums[i] > nums[i - 1]) {
58+
up = down + 1;
59+
} else if(nums[i] < nums[i - 1]) {
60+
down = up + 1;
61+
}
62+
}
63+
return max(up, down);
64+
}
2265
};

0 commit comments

Comments
 (0)