Skip to content

Commit c619b89

Browse files
Add files via upload
1 parent b267f99 commit c619b89

17 files changed

+1167
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Given N * M field of O's and X's, where O=white, X=black
3+
Return the number of black shapes. A black shape consists of one or more adjacent X's (diagonals not included)
4+
5+
Example:
6+
7+
OOOXOOO
8+
OOXXOXO
9+
OXOOOXO
10+
11+
answer is 3 shapes are :
12+
(i) X
13+
X X
14+
(ii)
15+
X
16+
(iii)
17+
X
18+
X
19+
Note that we are looking for connected shapes here.
20+
21+
For example,
22+
23+
XXX
24+
XXX
25+
XXX
26+
is just one single connected black shape.
27+
28+
LINK: https://www.interviewbit.com/problems/black-shapes/
29+
*/
30+
31+
int n,m;
32+
int X[] = {0, 0, 1, -1};
33+
int Y[] = {1, -1, 0, 0};
34+
35+
void dfs(int x, int y, vector<string> &A)
36+
{
37+
A[x][y] = 'O';
38+
39+
for(int i=0;i<4;i++)
40+
{
41+
int newx = x+X[i];
42+
int newy = y+Y[i];
43+
44+
if(newx>=0 && newx<n && newy>=0 && newy<m && A[newx][newy]=='X')
45+
dfs(newx,newy,A);
46+
}
47+
}
48+
49+
int Solution::black(vector<string> &A)
50+
{
51+
n = A.size();
52+
m = A[0].size();
53+
54+
int ans = 0;
55+
56+
for(int i=0;i<n;i++)
57+
for(int j=0;j<m;j++)
58+
if(A[i][j] == 'X')
59+
{
60+
ans++;
61+
dfs(i,j,A);
62+
}
63+
64+
return ans;
65+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
3+
4+
A region is captured by flipping all 'O's into 'X's in that surrounded region.
5+
6+
For example,
7+
8+
X X X X
9+
X O O X
10+
X X O X
11+
X O X X
12+
After running your function, the board should be:
13+
14+
X X X X
15+
X X X X
16+
X X X X
17+
X O X X
18+
19+
LINK: https://www.interviewbit.com/problems/capture-regions-on-board/
20+
*/
21+
22+
int m,n;
23+
vector<vector<bool> > vis;
24+
25+
int X[] = {0, 0, 1, -1};
26+
int Y[] = {1, -1, 0, 0};
27+
28+
void dfs(int x, int y, vector<vector<char> > &A)
29+
{
30+
vis[x][y] = true;
31+
32+
for(int i=0;i<4;i++)
33+
{
34+
int newx = x + X[i];
35+
int newy = y + Y[i];
36+
37+
if(newx>=0 && newx<m && newy>=0 && newy<n && vis[newx][newy]==false && A[newx][newy]=='O')
38+
dfs(newx,newy,A);
39+
}
40+
}
41+
42+
void Solution::solve(vector<vector<char> > &A)
43+
{
44+
// Do not write main() function.
45+
// Do not read input, instead use the arguments to the function.
46+
// Do not print the output, instead return values as specified
47+
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
48+
49+
m = A.size();
50+
n = A[0].size();
51+
52+
vis.clear();
53+
vis.resize(m, vector<bool>(n, false));
54+
55+
for(int i=0;i<m;i++)
56+
{
57+
if(A[i][0] == 'O' && vis[i][0] == false)
58+
dfs(i,0,A);
59+
if(A[i][n-1] == 'O' && vis[i][n-1] == false)
60+
dfs(i,n-1,A);
61+
}
62+
63+
for(int j=0;j<n;j++)
64+
{
65+
if(A[0][j] == 'O' && vis[0][j]==false)
66+
dfs(0,j,A);
67+
if(A[m-1][j] == 'O' && vis[m-1][j]==false)
68+
dfs(m-1,j,A);
69+
}
70+
71+
for(int i=0;i<m;i++)
72+
for(int j=0;j<n;j++)
73+
if(A[i][j] == 'O' && !vis[i][j])
74+
A[i][j] = 'X';
75+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
3+
4+
LINK: https://www.interviewbit.com/problems/clone-graph/
5+
*/
6+
7+
/**
8+
* Definition for undirected graph.
9+
* struct UndirectedGraphNode {
10+
* int label;
11+
* vector<UndirectedGraphNode *> neighbors;
12+
* UndirectedGraphNode(int x) : label(x) {};
13+
* };
14+
*/
15+
UndirectedGraphNode *Solution::cloneGraph(UndirectedGraphNode *node)
16+
{
17+
if(node == NULL)
18+
return node;
19+
20+
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> mp;
21+
queue<UndirectedGraphNode*> q;
22+
23+
UndirectedGraphNode* src = new UndirectedGraphNode(node->label);
24+
mp[node] = src;
25+
26+
q.push(node);
27+
28+
while(!q.empty())
29+
{
30+
UndirectedGraphNode* u = q.front();
31+
q.pop();
32+
33+
vector<UndirectedGraphNode*> v = u->neighbors;
34+
int n = v.size();
35+
36+
for(int i=0;i<n;i++)
37+
{
38+
if(mp[v[i]] == NULL)
39+
{
40+
UndirectedGraphNode* temp = new UndirectedGraphNode(v[i]->label);
41+
mp[v[i]] = temp;
42+
q.push(v[i]);
43+
}
44+
45+
mp[u]->neighbors.push_back(mp[v[i]]);
46+
}
47+
}
48+
49+
return src;
50+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
There are n islands and there are many bridges connecting them. Each bridge has some cost attached to it.
3+
4+
We need to find bridges with minimal cost such that all islands are connected.
5+
6+
It is guaranteed that input data will contain at least one possible scenario in which all islands are connected with each other.
7+
8+
Example :
9+
Input
10+
11+
Number of islands ( n ) = 4
12+
1 2 1
13+
2 3 4
14+
1 4 3
15+
4 3 2
16+
1 3 10
17+
In this example, we have number of islands(n) = 4 . Each row then represents a bridge configuration. In each row first two numbers represent the islands number which are connected by this bridge and the third integer is the cost associated with this bridge.
18+
19+
In the above example, we can select bridges 1(connecting islands 1 and 2 with cost 1), 3(connecting islands 1 and 4 with cost 3), 4(connecting islands 4 and 3 with cost 2). Thus we will have all islands connected with the minimum possible cost(1+3+2 = 6).
20+
In any other case, cost incurred will be more.
21+
22+
LINK: https://www.interviewbit.com/problems/commutable-islands/
23+
*/
24+
25+
int Rank[1000005];
26+
int Parent[1000005];
27+
28+
int Find(int x)
29+
{
30+
if(x!=Parent[x])
31+
Parent[x] = Find(Parent[x]);
32+
return Parent[x];
33+
}
34+
35+
int Union(int u, int v, int cost)
36+
{
37+
int x = Find(u);
38+
int y = Find(v);
39+
40+
if(x==y)
41+
return 0;
42+
43+
if(Rank[x] > Rank[y])
44+
{
45+
Parent[y] = x;
46+
Rank[x] += Rank[y];
47+
}
48+
else
49+
{
50+
Parent[x] = y;
51+
Rank[y] += Rank[x];
52+
}
53+
54+
return cost;
55+
}
56+
57+
bool comp(const vector<int> &a, const vector<int> &b)
58+
{
59+
return a[2] < b[2];
60+
}
61+
62+
int Solution::solve(int A, vector<vector<int> > &B)
63+
{
64+
for(int i=0;i<A;i++)
65+
{
66+
Parent[i] = i;
67+
Rank[i] = 1;
68+
}
69+
70+
sort(B.begin(), B.end(), comp);
71+
72+
int n = B.size();
73+
int ans = 0;
74+
75+
for(int i=0;i<n;i++)
76+
ans += Union(B[i][0]-1, B[i][1]-1, B[i][2]);
77+
78+
return ans;
79+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
3+
4+
A height balanced BST : a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
5+
Example :
6+
7+
8+
Given A : 1 -> 2 -> 3
9+
A height balanced BST :
10+
11+
2
12+
/ \
13+
1 3
14+
15+
LINK: https://www.interviewbit.com/problems/convert-sorted-list-to-binary-search-tree/
16+
*/
17+
18+
/**
19+
* Definition for binary tree
20+
* struct TreeNode {
21+
* int val;
22+
* TreeNode *left;
23+
* TreeNode *right;
24+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
25+
* };
26+
*/
27+
/**
28+
* Definition for singly-linked list.
29+
* struct ListNode {
30+
* int val;
31+
* ListNode *next;
32+
* ListNode(int x) : val(x), next(NULL) {}
33+
* };
34+
*/
35+
36+
TreeNode* convertToBST(ListNode** head, int n)
37+
{
38+
if(n<=0)
39+
return NULL;
40+
41+
TreeNode* left = convertToBST(head, n/2);
42+
43+
TreeNode* root = new TreeNode((*head)->val);
44+
root->left = left;
45+
46+
(*head) = (*head)->next;
47+
48+
root->right = convertToBST(head, n-n/2-1);
49+
50+
return root;
51+
}
52+
53+
TreeNode* Solution::sortedListToBST(ListNode* A)
54+
{
55+
// Do not write main() function.
56+
// Do not read input, instead use the arguments to the function.
57+
// Do not print the output, instead return values as specified
58+
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
59+
60+
int cnt = 0;
61+
ListNode* temp = A;
62+
63+
while(temp)
64+
{
65+
temp = temp->next;
66+
cnt++;
67+
}
68+
69+
return convertToBST(&A, cnt);
70+
}

0 commit comments

Comments
 (0)