Skip to content

Commit e8e04aa

Browse files
author
kaidul
committed
Google onsite interview completed. Feedback is positive from interviewers, waiting for hiring committee approval
1 parent 45f4591 commit e8e04aa

8 files changed

+912
-616
lines changed

README.md

+591-585
Large diffs are not rendered by default.

source-code/Best_Time_to_Buy_and_Sell_Stock_III.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,37 @@ class Solution {
6161
}
6262
return maxProfitUtils(0, 0, 0, profitFrom, prices, dp);
6363
}
64+
};
65+
66+
// another dp approach
67+
class Solution {
68+
int maxProfit(int indx, int stock, vector<int> const& prices, vector<vector<int>>& dp) {
69+
if (indx >= prices.size() - 1) {
70+
return 0;
71+
}
72+
if (stock == 2) {
73+
return 0;
74+
}
75+
if (dp[indx][stock] != -1) {
76+
return dp[indx][stock];
77+
}
78+
79+
int profit = 0;
80+
int minPrice = prices[indx];
81+
for (int i = indx + 1; i < prices.size(); i++) {
82+
if (prices[i] > minPrice) {
83+
profit = max(profit, (prices[i] - minPrice) + maxProfit(i, stock + 1, prices, dp));
84+
}
85+
minPrice = min(minPrice, prices[i]);
86+
}
87+
88+
return dp[indx][stock] = profit;
89+
}
90+
91+
public:
92+
int maxProfit(vector<int>& prices) {
93+
if (prices.empty()) return 0;
94+
vector<vector<int>> dp(prices.size(), vector<int>(3, -1));
95+
return maxProfit(0, 0, prices, dp);
96+
}
6497
};

source-code/Best_Time_to_Buy_and_Sell_Stock_IV.cpp

+23-31
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
1-
// Memory limit exceeded (the 2D dp array seems too big)
1+
// MLE for very big k and array size
22
class Solution {
3-
public:
4-
int maxProfitUtils(int cnt, int idx, int profit, int k, vector<int>& prices, vector<vector<int>>& dp) {
5-
if(cnt > k) {
6-
return INT_MAX;
3+
int maxProfit(int indx, int stock, int const k, vector<int> const& prices, vector<vector<int>>& dp) {
4+
if (indx >= prices.size() - 1) {
5+
return 0;
76
}
8-
if(idx == (int)prices.size() or cnt == k) {
9-
return profit;
7+
if (stock == k) {
8+
return 0;
9+
}
10+
if (dp[indx][stock] != -1) {
11+
return dp[indx][stock];
1012
}
11-
if(dp[idx][cnt] != -1) return dp[idx][cnt];
12-
13-
int profitUntil, profitUntilPrev;
14-
int Min = prices[idx];
15-
profitUntilPrev = 0;
16-
int ret = 0;
1713

18-
ret = max(ret, profit + maxProfitUtils(cnt, idx + 1, 0, k, prices, dp));
19-
for(int j = 1; j < (int)prices.size() - idx; ++j) {
20-
profitUntil = max(profitUntilPrev, prices[j + idx] - Min);
21-
if(prices[j + idx] - Min > 0) {
22-
ret = max(ret, profit + maxProfitUtils(cnt + 1, j + idx + 1, profitUntil, k, prices, dp));
23-
} else {
24-
ret = max(ret, profit + maxProfitUtils(cnt, j + idx + 1, profitUntil, k, prices, dp));
14+
int profit = 0;
15+
int minPrice = prices[indx];
16+
for (int i = indx + 1; i < prices.size(); i++) {
17+
if (prices[i] > minPrice) {
18+
profit = max(profit, (prices[i] - minPrice) + maxProfit(i, stock + 1, k, prices, dp));
2519
}
26-
profitUntilPrev = profitUntil;
27-
Min = min(Min, prices[j + idx]);
20+
minPrice = min(minPrice, prices[i]);
2821
}
29-
return dp[idx][cnt] = ret;
22+
23+
return dp[indx][stock] = profit;
3024
}
3125

32-
33-
int maxProfit(int k, vector<int> &prices) {
34-
int n = (int)prices.size();
35-
k = min(k, n / 2);
36-
vector< vector<int> > dp(n, vector<int>(k + 1, -1));
37-
if(n < 2) return 0;
38-
int r = maxProfitUtils(0, 0, 0, k, prices, dp);
39-
return r;
26+
public:
27+
int maxProfit(int k, vector<int>& prices) {
28+
if (prices.empty()) return 0;
29+
k = min(k, (int)prices.size() - 1);
30+
vector<vector<int>> dp(prices.size(), vector<int>(k + 1, -1));
31+
return maxProfit(0, 0, k, prices, dp);
4032
}
4133
};
4234

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
class Solution {
2+
int dx[4] = {-1, 0, 1, 0};
3+
int dy[4] = {0, 1, 0, -1};
4+
int totalBricksAttached(int x, int y, vector<vector<int>>& grid) {
5+
if (grid[x][y] != 1) {
6+
return 0;
7+
}
8+
grid[x][y] = 2;
9+
int count = 0;
10+
for (int i = 0; i < sizeof(dx) / sizeof(dx[0]); i++) {
11+
int neighX = x + dx[i];
12+
int neighY = y + dy[i];
13+
if (neighX >= 0 and neighY >= 0 and neighX < grid.size() and neighY < grid[0].size()
14+
and grid[neighX][neighY] == 1) {
15+
count += totalBricksAttached(neighX, neighY, grid);
16+
}
17+
}
18+
return 1 + count;
19+
}
20+
21+
bool isConnectedToTop(int x, int y, vector<vector<int>>& grid) {
22+
if (x == 0) {
23+
return true;
24+
}
25+
for (int i = 0; i < sizeof(dx) / sizeof(dx[0]); i++) {
26+
int neighX = x + dx[i];
27+
int neighY = y + dy[i];
28+
if (neighX >= 0 and neighY >= 0 and neighX < grid.size() and neighY < grid[0].size()
29+
and grid[neighX][neighY] == 2) {
30+
return true;
31+
}
32+
}
33+
return false;
34+
}
35+
36+
vector<int> searchFallingBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
37+
vector<int> result(hits.size(), 0);
38+
for (int i = hits.size() - 1; i >= 0; i--) {
39+
vector<int>& hit = hits[i];
40+
int x = hit[0], y = hit[1];
41+
if (grid[x][y] == 0) {
42+
grid[x][y] = 1;
43+
if (isConnectedToTop(x, y, grid)) {
44+
result[i] = totalBricksAttached(x, y, grid) - 1;
45+
}
46+
}
47+
}
48+
return result;
49+
}
50+
51+
void removeHits(vector<vector<int>>& grid, vector<vector<int>>& hits) {
52+
for (vector<int> hit : hits) {
53+
int x = hit[0], y = hit[1];
54+
grid[x][y] = grid[x][y] - 1;
55+
}
56+
}
57+
58+
void markBricks(vector<vector<int>>& grid) {
59+
for (int i = 0; i < grid[0].size(); i++) {
60+
totalBricksAttached(0, i, grid);
61+
}
62+
}
63+
public:
64+
vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
65+
removeHits(grid, hits);
66+
markBricks(grid);
67+
return searchFallingBricks(grid, hits);
68+
}
69+
};
70+
71+
// TLE
72+
class Solution {
73+
int dx[4] = {-1, 0, 1, 0};
74+
int dy[4] = {0, 1, 0, -1};
75+
int totalBricksAttached(vector<vector<int>>& grid, vector<vector<bool>>& isBrick, int x, int y) {
76+
isBrick[x][y] = true;
77+
int count = 0;
78+
for (int i = 0; i < sizeof(dx) / sizeof(dx[0]); i++) {
79+
int neighX = x + dx[i];
80+
int neighY = y + dy[i];
81+
if (neighX >= 0 and neighY >= 0 and neighX < grid.size() and neighY < grid[0].size()
82+
and grid[neighX][neighY] and !isBrick[neighX][neighY]) {
83+
isBrick[neighX][neighY] = true;
84+
count += totalBricksAttached(grid, isBrick, neighX, neighY);
85+
}
86+
}
87+
return 1 + count;
88+
}
89+
90+
int totalBricksAttached(vector<vector<int>>& grid, vector<vector<bool>>& isBrick) {
91+
int totalBricks = 0;
92+
for (int i = 0; i < grid[0].size(); i++) {
93+
if (grid[0][i] and !isBrick[0][i]) {
94+
totalBricks += totalBricksAttached(grid, isBrick, 0, i);
95+
}
96+
}
97+
return totalBricks;
98+
}
99+
public:
100+
vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
101+
vector<vector<bool>> isBrick;
102+
103+
isBrick = vector<vector<bool>>(grid.size(), vector<bool> (grid[0].size(), false));
104+
int totalBricks = totalBricksAttached(grid, isBrick);
105+
106+
vector<int> result;
107+
for (vector<int> hit : hits) {
108+
int x = hit[0], y = hit[1];
109+
if (!isBrick[x][y]) {
110+
result.push_back(0);
111+
continue;
112+
}
113+
grid[x][y] = 0;
114+
totalBricks--;
115+
isBrick = vector<vector<bool>>(grid.size(), vector<bool> (grid[0].size(), false));
116+
int fallenBricks = totalBricks - totalBricksAttached(grid, isBrick);
117+
result.push_back(fallenBricks);
118+
totalBricks -= fallenBricks;
119+
}
120+
121+
return result;
122+
}
123+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 isLevelComplete(int level, vector<int>& currLevelNodes) {
12+
return (int)currLevelNodes.size() == (1 << level);
13+
}
14+
15+
bool firstNodeIsLeftMost(int level, vector<int>& currLevelNodes) {
16+
if (currLevelNodes.empty()) {
17+
return true;
18+
}
19+
return currLevelNodes[0] == (1 << level);
20+
}
21+
public:
22+
bool isCompleteTree(TreeNode* root) {
23+
if (!root) {
24+
return true;
25+
}
26+
int level = 0;
27+
queue<pair<TreeNode*, pair<int, int>>> Q;
28+
Q.push({root, {level, 1}});
29+
vector<int> currLevelNodes;
30+
currLevelNodes.push_back(1);
31+
32+
while (!Q.empty()) {
33+
pair<TreeNode*, pair<int, int>> curr = Q.front();
34+
TreeNode* currNode = curr.first;
35+
int currLevel = curr.second.first;
36+
int nodeSeq = curr.second.second;
37+
Q.pop();
38+
if (level == 0 or currLevel > level) {
39+
if (!isLevelComplete(level, currLevelNodes)) {
40+
return false;
41+
}
42+
currLevelNodes = vector<int>();
43+
level = currLevel;
44+
}
45+
currLevelNodes.push_back(nodeSeq);
46+
if (currNode->left) {
47+
Q.push({currNode->left, {currLevel + 1, nodeSeq << 1}});
48+
}
49+
if (currNode->right) {
50+
Q.push({currNode->right, {currLevel + 1, (nodeSeq << 1) | 1}});
51+
}
52+
}
53+
54+
// last level
55+
if (!isLevelComplete(level, currLevelNodes)) {
56+
if (!firstNodeIsLeftMost(level, currLevelNodes)) {
57+
return false;
58+
}
59+
for(int i = 0; i < (int)currLevelNodes.size() - 1; i++) {
60+
if (currLevelNodes[i + 1] != currLevelNodes[i] + 1) {
61+
return false;
62+
}
63+
}
64+
}
65+
return true;
66+
}
67+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
13+
if (!root1 and !root2) {
14+
return true;
15+
}
16+
if (!root1 or !root2) {
17+
return false;
18+
}
19+
return root1->val == root2->val
20+
and (
21+
(flipEquiv(root1->left, root2->left) and flipEquiv(root1->right, root2->right))
22+
or (flipEquiv(root1->left, root2->right) and flipEquiv(root1->right, root2->left))
23+
);
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int minAddToMakeValid(string S) {
4+
int balance = 0;
5+
int parenthesesNeeded = 0;
6+
for (char parentheses : S) {
7+
balance += (parentheses == '(') ? +1 : -1;
8+
if (balance < 0) {
9+
++balance;
10+
++parenthesesNeeded;
11+
}
12+
}
13+
if (balance > 0) {
14+
parenthesesNeeded += balance;
15+
}
16+
17+
return parenthesesNeeded;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
bool isAlienSorted(vector<string>& words, string seq) {
4+
vector<int> order(256);
5+
int k = 0;
6+
for (char ch : seq) {
7+
order[ch] = ++k;
8+
}
9+
for (int i = 0; i < words.size() - 1; i++) {
10+
string word1 = words[i];
11+
string word2 = words[i + 1];
12+
bool equal = true;
13+
for (int j = 0; j < min(word1.length(), word2.length()); j++) {
14+
if (word1[j] == word2[j]) {
15+
continue;
16+
}
17+
if (order[word1[j]] < order[word2[j]]) {
18+
equal = false;
19+
break;
20+
}
21+
22+
return false;
23+
}
24+
if (equal and word1.length() > word2.length()) {
25+
return false;
26+
}
27+
}
28+
29+
return true;
30+
}
31+
};

0 commit comments

Comments
 (0)