Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRAPH_LIVE_YOUTUBE - 3 problems #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions LeetCode/1387.sort-integers-by-the-power-value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
unordered_map<int, int> power; // O(1) lookup, O(N) extra space

int get_power(int n) {
if(n == 1) return 0;
if(power[n] != 0) return power[n];
if (n&1) {
return power[n] = 1 + get_power(3*n + 1);
}
else {
return power[n] = 1 + get_power(n/2);
}
}

bool cmp(int x, int y) {
int px = get_power(x);
int py = get_power(y);
if (px == py) return x < y;
return px < py;
}

class Solution {
public:
// let n: hi-lo
int getKth(int lo, int hi, int k) {
// power of 178 -- x steps to reach 380 -- x+z
// power of 239 -- y steps to reach 380 -- y+z
// both eventually meet at 380 -- z steps to reach 1
vector<int> arr; // O(n) extra space
for(int i = lo; i <= hi; i++) {
arr.push_back(i);
}
sort(arr.begin(), arr.end(), cmp); // O(nlogn) time
return arr[k-1];
}
};

/*
- ranges
- inclusive: [ ]
- exclusive: ()
(1, 5) -> 2,3,4
[1, 5) -> 1,2,3,4


*/
18 changes: 18 additions & 0 deletions LeetCode/1557.minimum-number-of-vertices-to-reach-all-nodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ass Solution {
public:
vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) {
vector<int> incoming(n+1, 0); //O(n) extra space

for(auto edge: edges) { // O(M) where M is # of edges
int u = edge[0], v = edge[1];
// u --> v
incoming[v]++; // O(logn) if you use maps
}

vector<int> ans = {}; // # of nodes with 0 incoming edges
for(int i = 0; i < n; i++) {
if (incoming[i] == 0) ans.push_back(i);
}
return ans;
}
};
76 changes: 76 additions & 0 deletions LeetCode/959.regions-cut-by-slashes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
int f(int i, int j, int n) {
return i * n + j;
}
const int N = 1000;
int deg[N];
class Solution {
public:
int regionsBySlashes(vector<string>& grid) {
int n = grid.size();
int e = 4 * n;
int n_dash = n+1; // # of nodes / points in each row in new graph
int v_dash = n_dash * n_dash;
// vector<vector<int>> corners = {
// {0, 0}, {0, n_dash-1},
// {n_dash-1, 0}, {n_dash-1, n_dash-1}
// };
for(int i = 0; i < n_dash; i++) {
int id = f(0, i, n_dash);
// cout << "Corner " << id << endl;
deg[id]++;

// right most column
id = f(i, n_dash-1, n_dash);
deg[id]++;

// bottom row
id = f(n_dash-1, i, n_dash);
deg[id]++;

// left most column
id = f(i, 0, n_dash);
deg[id]++;
}

for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
char c = grid[i][j];
if(c == '/') {
int u = f(i, j+1, n_dash), v = f(i+1, j, n_dash);
// cout << u << " " << v << endl;
deg[u]++, deg[v]++;
e++;
}
else if (c == '\\') {
int u = f(i, j, n_dash), v = f(i+1, j+1, n_dash);
// cout << u << " " << v << endl;
deg[u]++, deg[v]++;
e++;
}
}
}
for(int i = 0; i < n_dash; i++) {
for(int j = 0; j < n_dash; j++) {
int id = f(i, j, n_dash); // node id in new vertex
if(deg[id] == 0) v_dash--;
deg[id] = 0; // clear the deg array for next run
}
}
return e - v_dash + 1;
}
};

/*
(0,0) (0,n_dash-1)
(n_dash-1, 0) (n_dash-1, n_dash-1)
x--x--x
| /|
x x x
| / |
x--x--x

f = e - v + 1
v is not all nodes - remove the nodes with 0 edges
edges and vertices --- # of regions / cycles
*/