Skip to content

Commit 47dff7e

Browse files
committed
🚀 29-Jul-2022
1 parent 4c7af5b commit 47dff7e

18 files changed

+3042
-161
lines changed

CPPNotes.md

Lines changed: 605 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Given a list of lowercase alphabetical strings words,
2+
return the maximum sum of the lengths of two distinct words that don't share a common letter.
3+
4+
Constraints
5+
6+
n = 1,000 where n is the length of words
7+
w = 1,000 where w is the length of the longest string in words
8+
Example 1
9+
Input
10+
words = ["abcde", "xyz", "abdexyz", "axyz"]
11+
Output
12+
8
13+
Explanation
14+
These two words don't share any common letters ["abcde", "xyz"]
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
// Brute Force (TLE)
26+
27+
int solve(vector<string>& words) {
28+
int sum = 0;
29+
30+
for (int i = 0; i < words.size(); i++) {
31+
unordered_map <char, bool> freq;
32+
for (auto &x: words[i]) freq[x] = true;
33+
34+
for (int j = i + 1; j < words.size(); j++) {
35+
bool same = false;
36+
for (auto &x: words[j]) {
37+
if (freq[x]) {
38+
same = true; break;
39+
}
40+
}
41+
if (!same) {
42+
int tmpSum = words[i].length() + words[j].length();
43+
sum = max(sum, tmpSum);
44+
}
45+
}
46+
}
47+
return sum;
48+
}
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
// Bitmasking (Accepted)
59+
60+
int getBitValue(string word) {
61+
int value = 0;
62+
for (auto &c: word) {
63+
int j = (c - 'a');
64+
value = value | (1 << j);
65+
}
66+
return value;
67+
}
68+
69+
int solve(vector<string>& words) {
70+
int sum = 0, n = words.size();
71+
vector <int> bitRepresentation;
72+
73+
for (auto &word: words) {
74+
bitRepresentation.push_back(getBitValue(word));
75+
}
76+
77+
for (int i = 0; i < n; i++) {
78+
for (int j = i + 1; j < n; j++) {
79+
if (int(bitRepresentation[i] & bitRepresentation[j]) == 0) {
80+
int tmpSum = words[i].length() + words[j].length();
81+
sum = max(sum, tmpSum);
82+
}
83+
}
84+
}
85+
return sum;
86+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
Given an n x n binary matrix grid, return the length of the shortest
2+
clear path in the matrix. If there is no clear path, return -1.
3+
4+
A clear path in a binary matrix is a path from the top-left cell
5+
(i.e., (0, 0)) to the bottom-right cell (i.e., (n - 1, n - 1)) such that:
6+
7+
All the visited cells of the path are 0.
8+
All the adjacent cells of the path are 8-directionally connected
9+
(i.e., they are different and they share an edge or a corner).
10+
The length of a clear path is the number of visited cells of this path.
11+
12+
13+
14+
Example 1:
15+
16+
17+
Input: grid = [[0,1],[1,0]]
18+
Output: 2
19+
Example 2:
20+
21+
22+
Input: grid = [[0,0,0],[1,1,0],[1,1,0]]
23+
Output: 4
24+
Example 3:
25+
26+
Input: grid = [[1,0,0],[1,1,0],[1,1,0]]
27+
Output: -1
28+
29+
30+
Constraints:
31+
32+
n == grid.length
33+
n == grid[i].length
34+
1 <= n <= 100
35+
grid[i][j] is 0 or 1
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
/*
46+
Here DFS give TLE because we search for every path and then take min out of all that paths
47+
But BFS work ?? why ?
48+
because we do traverse in DFS manner so first time when we get target cell a[n-1][n-1]
49+
then this path is always minimum as we do DFS
50+
51+
Both solution are mentioned below
52+
*/
53+
54+
55+
// dfs (TLE)
56+
57+
class Solution {
58+
public:
59+
int shortestPath = INT_MAX;
60+
vector <vector<bool>> vis;
61+
vector <pair<int, int>> directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
62+
63+
bool isValid(int i, int j, vector<vector<int>> &grid) {
64+
return (i >= 0 && i < grid.size() && j >= 0 && j < grid.size() && !vis[i][j] && grid[i][j] == 0);
65+
}
66+
67+
void dfs(int i, int j, int path, vector<vector<int>> &grid) {
68+
if (i == grid.size() - 1 && j == grid.size() - 1) {
69+
path++;
70+
shortestPath = min(shortestPath, path);
71+
return;
72+
}
73+
74+
vis[i][j] = true;
75+
76+
for (auto direction: directions) {
77+
int x = i + direction.first;
78+
int y = j + direction.second;
79+
if (isValid(x, y, grid)) dfs(x, y, path + 1, grid);
80+
}
81+
82+
vis[i][j] = false; // backtrack
83+
}
84+
85+
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
86+
int n = grid.size();
87+
vis.resize(n, vector<bool>(n, false));
88+
89+
if (grid[0][0] != 0 || grid[n - 1][n - 1] != 0) return -1; // no path possible
90+
91+
dfs(0, 0, 0, grid);
92+
93+
return shortestPath != INT_MAX ? shortestPath : -1;
94+
}
95+
};
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
// bfs
106+
107+
class Solution {
108+
public:
109+
int shortestPath = INT_MAX;
110+
vector <vector<bool>> vis;
111+
vector <pair<int, int>> directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
112+
113+
bool isValid(int i, int j, vector<vector<int>> &grid) {
114+
return (i >= 0 && i < grid.size() && j >= 0 && j < grid.size() && !vis[i][j] && grid[i][j] == 0);
115+
}
116+
117+
void bfs(int i, int j, int path, vector<vector<int>> &grid) {
118+
queue <pair<int, int>> q;
119+
120+
q.push({i, j});
121+
122+
while (!q.empty()) {
123+
int size = q.size();
124+
path++;
125+
126+
while (size--) {
127+
128+
i = q.front().first;
129+
j = q.front().second;
130+
q.pop();
131+
132+
if (i == grid.size() - 1 && j == grid.size() - 1) {
133+
shortestPath = min(shortestPath, path);
134+
return;
135+
}
136+
137+
for (auto direction: directions) {
138+
int x = i + direction.first;
139+
int y = j + direction.second;
140+
141+
if (isValid(x, y, grid)) {
142+
vis[x][y] = 1; // mark visited
143+
// here we don't mark vis[i][j] as visted
144+
q.push({x, y});
145+
}
146+
}
147+
}
148+
}
149+
}
150+
151+
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
152+
int n = grid.size();
153+
vis.resize(n, vector<bool>(n, false));
154+
155+
if (grid[0][0] != 0 || grid[n - 1][n - 1] != 0) return -1; // no path possible
156+
157+
bfs(0, 0, 0, grid);
158+
159+
return shortestPath != INT_MAX ? shortestPath : -1;
160+
}
161+
};
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
// OPtimization
172+
// We can use the grid to make track of the visited nodes, update grid[i][j] = 1,
173+
// so that we don't visite it again
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
There are n servers numbered from 0 to n - 1 connected by undirected server-to-server
2+
connections forming a network where connections[i] = [ai, bi] represents a connection
3+
between servers ai and bi. Any server can reach other servers directly or indirectly through the network.
4+
5+
A critical connection is a connection that, if removed, will make some servers unable to reach some other server.
6+
7+
Return all critical connections in the network in any order.
8+
9+
10+
Example 1:
11+
12+
13+
Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
14+
Output: [[1,3]]
15+
Explanation: [[3,1]] is also accepted.
16+
Example 2:
17+
18+
Input: n = 2, connections = [[0,1]]
19+
Output: [[0,1]]
20+
21+
22+
Constraints:
23+
24+
2 <= n <= 105
25+
n - 1 <= connections.length <= 105
26+
0 <= ai, bi <= n - 1
27+
ai != bi
28+
There are no repeated connections.
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
// Approach 1: Brute Force
40+
// Create a graph, traverse over the edges and remove the current edge from the graph
41+
// and check via a dfs traversal whether all the nodes are still rechable from any node to any other
42+
// if not, then it's a critical connection/
43+
// TC -> O(E(V + E))
44+
45+
46+
47+
// Approach 2:
48+
// TC -> O(V + E)
49+
50+
class Solution {
51+
public:
52+
vector <bool> visited;
53+
vector <int> disc, low, parent;
54+
vector<vector<int>> criticalConn;
55+
int id = 0;
56+
57+
void dfs(int u, vector <int> graph[]) {
58+
visited[u] = true;
59+
disc[u] = id;
60+
low[u] = id;
61+
id++;
62+
63+
for (auto v: graph[u]) {
64+
if (!visited[v]) {
65+
parent[v] = u;
66+
dfs(v, graph);
67+
low[u] = min(low[u], low[v]);
68+
69+
if (low[v] > disc[u]) {
70+
criticalConn.push_back({u, v});
71+
}
72+
} else if (v != parent[u]) {
73+
low[u] = min(low[u], disc[v]);
74+
}
75+
}
76+
77+
}
78+
79+
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
80+
vector <int> graph[n];
81+
82+
for (auto connection: connections) {
83+
graph[connection[0]].push_back(connection[1]);
84+
graph[connection[1]].push_back(connection[0]);
85+
}
86+
87+
visited.resize(n, false);
88+
disc.resize(n, 0);
89+
low.resize(n, 0);
90+
parent.resize(n, -1);
91+
92+
for (int i = 0; i < n; i++) {
93+
if (!visited[i]) dfs(i, graph);
94+
}
95+
96+
return criticalConn;
97+
}
98+
};

0 commit comments

Comments
 (0)