Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 47d987d

Browse files
author
kaidul
committed
Facebook Interview: 2.0 preparation
1 parent 26bd72f commit 47d987d

11 files changed

+976
-591
lines changed

‎README.md‎

Lines changed: 596 additions & 587 deletions
Large diffs are not rendered by default.

‎readme-generator.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ int main(void) {
4343
perror ("Directory open failed.");
4444
return EXIT_FAILURE;
4545
}
46+
printf("Success!");
4647
return EXIT_SUCCESS;
4748
}

‎source-code/01_Matrix.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Solution {
2727
}
2828
}
2929
}
30-
30+
3131
return matrix;
3232
}
33-
};
33+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
int boundaryIndx(vector<int>& arr, int left, int target) {
3+
int right = arr.size() - 1;
4+
int indx = left;
5+
while(left <= right) {
6+
int mid = left + (right - left) / 2;
7+
if(target < arr[mid]) {
8+
left = mid + 1;
9+
indx = mid;
10+
} else {
11+
right = mid - 1;
12+
}
13+
}
14+
return indx;
15+
}
16+
public:
17+
int numFriendRequests(vector<int>& ages) {
18+
int result = 0;
19+
sort(ages.begin(), ages.end(), greater<int>());
20+
for(int i = 0; i < ages.size(); i++) {
21+
int bAge = ages[i] / 2 + 7;
22+
int boundary = boundaryIndx(ages, i, bAge);
23+
int frndReq = (boundary - i);
24+
result += frndReq;
25+
while(i + 1 < ages.size() and ages[i] == ages[i + 1]) {
26+
result += frndReq;
27+
i++;
28+
}
29+
}
30+
return result;
31+
}
32+
};

‎source-code/Is_Graph_Bipartite?.cpp‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
enum Color {
3+
WHITE = 1,
4+
BLACK = 2
5+
};
6+
7+
bool isBipartite(int node, vector<vector<int>> const& graph, vector<int>& color) {
8+
for(int i = 0; i < graph[node].size(); i++) {
9+
int neigh = graph[node][i];
10+
if(color[neigh] and color[neigh] == color[node]) {
11+
return false;
12+
}
13+
if(!color[neigh]) {
14+
color[neigh] = (color[node] == WHITE ? BLACK : WHITE);
15+
if(!isBipartite(neigh, graph, color)) {
16+
return false;
17+
}
18+
}
19+
}
20+
return true;
21+
}
22+
23+
public:
24+
bool isBipartite(vector<vector<int>>& graph) {
25+
int n = (int)graph.size();
26+
vector<int> color(n, 0);
27+
for(int node = 0; node < n; node++) {
28+
if(!color[node]) {
29+
color[node] = WHITE;
30+
if(!isBipartite(node, graph, color)) {
31+
return false;
32+
}
33+
}
34+
}
35+
return true;
36+
}
37+
};

‎source-code/Largest_Plus_Sign.cpp‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class Solution {
2+
public:
3+
int orderOfLargestPlusSign(int N, vector<vector<int>>& mines) {
4+
vector<vector<int>> left(N, vector<int>(N, 1));
5+
vector<vector<int>> right(left), up(left), down(left);
6+
7+
for(int i = 0; i < mines.size(); i++) {
8+
int x = mines[i][0], y = mines[i][1];
9+
left[x][y] = right[x][y] = up[x][y] = down[x][y] = 0;
10+
}
11+
12+
for(int i = 0; i < N; i++) {
13+
for(int j = 0, k = N - 1; j < N; j++, k--) {
14+
if(left[i][j] and j > 0) {
15+
left[i][j] += left[i][j - 1];
16+
}
17+
if(right[i][k] and k + 1 < N) {
18+
right[i][k] += right[i][k + 1];
19+
}
20+
if(up[j][i] and j > 0) {
21+
up[j][i] += up[j - 1][i];
22+
}
23+
if(down[k][i] and k + 1 < N) {
24+
down[k][i] += down[k + 1][i];
25+
}
26+
}
27+
}
28+
29+
int result = 0;
30+
for(int i = 0; i < N; i++) {
31+
for(int j = 0, l = N - 1; j < N; j++, l--) {
32+
if(left[i][j]) {
33+
int k = left[i][j] / 2;
34+
35+
int centerX = i, centerY = j - k;
36+
if(centerX - k >= 0 and centerX + k < N and centerY - k >= 0) {
37+
bool plus = min(min(right[centerX][centerY - k], up[centerX + k][centerY]), down[centerX - k][centerY]) >= left[i][j];
38+
if(plus) {
39+
int order = k + 1;
40+
result = max(result, order);
41+
}
42+
}
43+
}
44+
45+
if(right[i][l]) {
46+
int k = right[i][l] / 2;
47+
48+
int centerX = i, centerY = l + k;
49+
if(centerX - k >= 0 and centerX + k < N and centerY + k < N) {
50+
bool plus = min(min(left[centerX][centerY + k], up[centerX + k][centerY]), down[centerX - k][centerY]) >= right[i][l];
51+
if(plus) {
52+
int order = k + 1;
53+
result = max(result, order);
54+
}
55+
}
56+
}
57+
58+
if(up[j][i]) {
59+
int k = up[j][i] / 2;
60+
61+
int centerX = j - k, centerY = i;
62+
if(centerX - k >= 0 and centerY + k < N and centerY - k >= 0) {
63+
bool plus = min(min(right[centerX][centerY - k], left[centerX][centerY + k]), down[centerX - k][centerY]) >= up[j][i];
64+
if(plus) {
65+
int order = k + 1;
66+
result = max(result, order);
67+
}
68+
}
69+
}
70+
71+
if(down[l][i]) {
72+
int k = down[l][i] / 2;
73+
74+
int centerX = l + k, centerY = i;
75+
if(centerX + k < N and centerX + k < N and centerY - k >= 0) {
76+
bool plus = min(min(right[centerX][centerY - k], left[centerX][centerY + k]), up[centerX + k][centerY]) >= down[l][i];
77+
if(plus) {
78+
int order = k + 1;
79+
result = max(result, order);
80+
}
81+
}
82+
}
83+
}
84+
}
85+
return result;
86+
}
87+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
void letterCasePermutation(int indx, string& S, vector<string>& result) {
3+
if(indx == S.length()) {
4+
result.push_back(S);
5+
return;
6+
}
7+
if(isalpha(S[indx])) {
8+
S[indx] = islower(S[indx]) ? toupper(S[indx]) : tolower(S[indx]);
9+
letterCasePermutation(indx + 1, S, result);
10+
S[indx] = islower(S[indx]) ? toupper(S[indx]) : tolower(S[indx]);
11+
}
12+
letterCasePermutation(indx + 1, S, result);
13+
}
14+
15+
public:
16+
vector<string> letterCasePermutation(string S) {
17+
vector<string> result;
18+
letterCasePermutation(0, S, result);
19+
return result;
20+
}
21+
};

‎source-code/Maximum_Product_Subarray.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Solution {
22
public:
3-
int maxProduct(int A[], int n) {
3+
int maxProduct(vector<int> A) {
4+
int n = (int)A.size();
45
int res = A[0];
56
int pos = max(0, A[0]);
67
int neg = min(0, A[0]);
@@ -20,4 +21,4 @@ class Solution {
2021
}
2122
return res;
2223
}
23-
};
24+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
bool isSwappable(int indx, vector<int>& A, vector<int>& B) {
3+
return indx == 0 or (indx > 0 and A[indx] > B[indx - 1] and B[indx] > A[indx - 1]);
4+
}
5+
6+
bool isOkay(int indx, vector<int>& A, vector<int>& B) {
7+
return indx == 0 or (indx > 0 and A[indx] > A[indx - 1] and B[indx] > B[indx - 1]);
8+
}
9+
10+
int minSwap(int indx, bool swapped, vector<int>& A, vector<int>& B, vector<vector<int>>& dp) {
11+
if(indx == (int)A.size()) {
12+
return 0;
13+
}
14+
15+
if(dp[indx][swapped] != -1) {
16+
return dp[indx][swapped];
17+
}
18+
19+
int ret = INT_MAX;
20+
21+
if(isOkay(indx, A, B)) {
22+
if(isSwappable(indx, A, B)) {
23+
swap(A[indx], B[indx]);
24+
ret = min(ret, 1 + minSwap(indx + 1, true, A, B, dp));
25+
swap(A[indx], B[indx]);
26+
}
27+
ret = min(ret, minSwap(indx + 1, false, A, B, dp));
28+
}
29+
30+
if(isSwappable(indx, A, B)) {
31+
swap(A[indx], B[indx]);
32+
ret = min(ret, 1 + minSwap(indx + 1, true, A, B, dp));
33+
swap(A[indx], B[indx]);
34+
}
35+
36+
return dp[indx][swapped] = ret;
37+
}
38+
39+
public:
40+
int minSwap(vector<int>& A, vector<int>& B) {
41+
int n = (int)A.size();
42+
vector<vector<int>> dp(n, vector<int>(2, -1));
43+
return minSwap(0, 0, A, B, dp);
44+
}
45+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int countCornerRectangles(vector<vector<int>>& grid) {
4+
int rows = (int)grid.size();
5+
int cols = (int)grid.at(0).size();
6+
vector<vector<int>> columnPair(rows, vector<int>(cols, 0));
7+
int result = 0;
8+
for(int i = 0; i < rows; i++) {
9+
for(int j = 0; j < cols; j++) {
10+
if(grid[i][j]) {
11+
for(int k = j + 1; k < cols; k++) {
12+
if(grid[i][k]) {
13+
result += columnPair[j][k];
14+
columnPair[j][k]++;
15+
}
16+
}
17+
}
18+
}
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /