Open nuoxoxo opened 5 months ago
:date: | :clipboard: | |
---|---|---|
30 | 1579. Remove Max Number of Edges to Keep Graph Fully Traversable | 🔴 |
Graph Union find OOP - classic UnionFind |
||
29 | 2192. All Ancestors of a Node in a Directed Acyclic Graph | 🟡 |
Graph - classic Toposort - DFS done in cc |
/*
[0,3],[0,4],
[1,3],
[2,4],[2,7],
[3,5],[3,6],[3,7],
[4,6]
- look at all 2nd-positions: 0, 1, 2 have no ancestors
- from (0,3) (1,3) we know 3 has two: 0, 1
- from (0,4) (2,4) we know 4 has two: 0, 2
- but these having direct ancestors are coincident
- for eg. 5 has 3 ancestors:
- degree-1 ---> 3
- degree-2 ---> 0,1
- we can think this way: [5]--1-->[3](0,1) so 1+2
- 6 has 5 ancestors:
- degree-1 ---> 3,4
- degree-2 ---> 0,1,2
- we can think this way:
- [6]--1-->[3](0,1)
- [6]--1-->[4](0,2) so a set(3,4,0,1,2)
- 7 has 4:
- [7]--1-->[2]
- [7]--1-->[3](0,1) so a set(2,3,0,1)
*/
#define vi vector<int>
#define si set<int>
// for each child of someone, save its direct parent
vector<vi> ADJ(n, vi{});
for (auto e : E)
{
int src = e[0], des = e[1];
ADJ[ des ].push_back( src );
}
// def. DFS
std::function<void(int, si &, si &)> DFS;
DFS = [&](int node, si & SEEN, si & path){
if (SEEN.find(node) == end(SEEN))
{
SEEN.insert(node);
path.insert(node);
for (int next: ADJ[node])
{
if (SEEN.find(next) == end(SEEN))
DFS(next, SEEN, path);
}
}
};
// DFS - get to the bottom of each nodev ~ [0,n-1]
int node = -1;
vector<vi> res;
while (++node < n)
{
si SEEN;
si parents;
for (int parent : ADJ[node])
{
if (SEEN.find(parent) == end(SEEN))
DFS(parent, SEEN, parents);
}
res.push_back(vector<int>(begin(parents), end(parents)));
}
return res;
// Subarray Sum Equals K
/*
a subarr can be expressed this way
-> a0 + a1 + a2 + ... + a[i - 1] + ai + ... + aj
-> ^^^^^^^^^^^^^ = f(j) - f(i - 1)
This is because in terms of line segment
-> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = f(j)
-> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = f(i - 1)
Conclusion
a subarr sums to k IF :
-> f(j) - f(i - 1) === k, ... from which we can have
-> f(j) - k === f(i - 1) ... this is what we want
-> so, keeping track of DICT[f(j) - k] == counting how many such subarrays
*/
// Subarray Sums Divisible by K
/*
a subarr sums 'divisible by k' IF :
-> (f(j) - f(i - 1)) === 0
-> f(j) % k === f(i - 1) % k ... this is what we want
-> so, keeping track of DICT[f(j) - k] == counting how many such subarrays
*/
:date: | :card_index: | |
---|---|---|
8 | 523. Continuous Subarray Sum | 🟡 |
- set - logic: Prefix Sum |
||
7 | 648. Replace Words | 🟡 |
trie |
||
6 | 846. Hand of Straights | 🟡 |
- idea: sorted dict keys | ||
5 | 1002. Find Common Characters | 🟢 |
4 | 409. Longest Palindrome | 🟡 |
2131. Longest Palindrome by Concatenating Two Letter Words 🆕 | 🟡 | |
- really weird : map works but unordered_map does not |
||
- might come back later on this | ||
3 | 2486. Append Characters to String to Make Subsequence | 🟡 |
2 | 344. Reverse String | 🟢 |
1 | 3110. Score of a String | 🟢 |