Skip to content

Commit 659d45b

Browse files
committed
GRAPH_LIVE_YOUTUBE - 3 problems
https://www.youtube.com/watch?v=4B0M0biIa3E
1 parent 6b7d3ad commit 659d45b

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const int N = 70;
2+
set<int> g[N];
3+
bool vis[N];
4+
5+
void fill_cluster_size(int u, int &cluster_size) {
6+
vis[u] = 1;
7+
// cout << "dfs::at couple " << u << endl;
8+
cluster_size++;
9+
for(int v: g[u]) {
10+
if(!vis[v]) {
11+
fill_cluster_size(v, cluster_size);
12+
}
13+
}
14+
}
15+
16+
int get_couple_id(int person) {
17+
return person / 2;
18+
}
19+
20+
21+
class Solution {
22+
public:
23+
int minSwapsCouples(vector<int>& row) {
24+
int n = row.size() / 2;
25+
// normalizing the seating arrangement in terms of couple ids
26+
for(int &x: row) x = get_couple_id(x);
27+
28+
for(int i = 0; i < n; i++) {
29+
g[i].clear();
30+
vis[i] = 0;
31+
}
32+
33+
for(int i = 0; i < row.size(); i += 2) {
34+
int c1 = row[i], c2 = row[i+1];
35+
if( c1 == c2 ) continue;
36+
g[c1].insert(c2);
37+
g[c2].insert(c1);
38+
// cout << i << " adding edge in couple " << c1 << " " << c2 << endl;
39+
}
40+
41+
int ans = 0;
42+
for(int i = 0; i < n; i++) {
43+
if(!vis[i]) {
44+
int cluster_size = 0;
45+
fill_cluster_size(i, cluster_size);
46+
ans += cluster_size - 1;
47+
}
48+
}
49+
return ans;
50+
}
51+
};
52+
/*
53+
0,3,1,2 -> 0,1,3,2
54+
easy notation: 0,1,0,1
55+
- Find a swap that can make two couples happy.
56+
-
57+
(0, 1) --- 1st couple --- couple id = 0
58+
(2, 3) --- 2nd couple --- couple id = 1
59+
(4, 5) -- 3rd couple --- couple id = 2
60+
0,3,5,2,1,4 --> 0,1,2,1,0,2
61+
62+
0---1---2
63+
\______/
64+
65+
x----y <--- 1 swap
66+
x----y----z <--- 2 swaps
67+
68+
C couples --- C-1 swaps
69+
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const int N = 20;
2+
vector<int> g[N];
3+
using path = vector<int>;
4+
set<path> get_all_paths(int src, int tar) {
5+
// cout << src << endl;
6+
if(src == tar) return { {tar} };
7+
set<path> ans = {};
8+
for(int v: g[src]) {
9+
auto rem_paths = get_all_paths(v, tar);
10+
for (path p: rem_paths) {
11+
p.push_back(src);
12+
ans.insert(p);
13+
}
14+
}
15+
return ans;
16+
}
17+
18+
19+
class Solution {
20+
21+
public:
22+
23+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
24+
// paths from 0 to 4
25+
// iterate on children v of 0
26+
// find paths from child v to 4
27+
// [0,1,3,4]
28+
int n = graph.size();
29+
30+
if(n == 0) return {};
31+
for(int i = 0; i < n; i++) g[i].clear();
32+
33+
for(int i = 0; i < n; i++) {
34+
for(int v: graph[i]) {
35+
g[i].push_back(v);
36+
}
37+
}
38+
39+
vector<path> ans = {};
40+
set<path> paths = get_all_paths(0, n-1);
41+
42+
43+
// O(# of paths * avg_size of path)
44+
for(auto p: paths) {
45+
reverse(p.begin(), p.end());
46+
ans.push_back(p);
47+
}
48+
49+
return ans;
50+
}
51+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int findJudge(int n, vector<vector<int>>& trust) {
4+
vector<int> followers(n+1, 0); // 1 based indexing
5+
vector<int> does_follow(n+1, 0);
6+
7+
map<int, int> followers, does_follow; // O(logN)
8+
9+
for(auto edge: trust) {
10+
int u = edge[0], v = edge[1];
11+
// person u trusts v
12+
followers[v]++; // O(1) v/s in maps O(logN) --> O(1)
13+
does_follow[u] = 1;
14+
}
15+
16+
vector<int> final_candidates = {};
17+
for(int i = 1; i <= n; i++) {
18+
if(followers[i] == n-1 && does_follow[i] == 0) {
19+
final_candidates.push_back(i);
20+
}
21+
}
22+
23+
if(final_candidates.size() != 1) return -1;
24+
return final_candidates[0];
25+
}
26+
};

0 commit comments

Comments
 (0)