This repository contains my solution for the coursera course Algorithm I & II
Knuth’s method: when reading the ith word, select it with probability 1/i to be the champion, replacing the previous champion. After reading all of the words, print the surviving champion.
How to deal with backwash: 
top and bottom respectively. When top and bottom is connected, those open sites that connected with bottom will also connect with top, just like left corner of the image.top and bottom, for percolation evaluation. Another has only one virtual site, top, for full evaluation.In the ideal world the probability of each string of making it to the output is K/N. Though we do not know N until the whole input stream is read. For large N and small K we certainly do not want to accumulate all N strings before choosing K of them. The method I used in my code is to fill the first K spots in the RandomizedQueue with the first K strings. Then Mth string in the input replaces (via dequeue/enqueue) one of the strings already in the RandomizedQueue with the probability K/M.
while (!StdIn.isEmpty()) {
String s = StdIn.readString();
cnt++;
if (queue.size() == k) {
if (StdRandom.bernoulli(1.0 * k / cnt)) {
queue.dequeue();
queue.enqueue(s);
}
}
else
queue.enqueue(s);
}
When sorting
pointsarray in slope order, make sure that it also remains natural order. Then, by only counting segments starts from the smallest point, we can remove duplicates.
lets say p1, p2, p3, p4, p5 forms a segments in natural order. p1 is the smallest, p5 is the largest (natural order)
p1 is slope anchor, min is p1, max is p5, add this segment.p2 is slope anchor, min is p1 not p2, max is p5, discard this duplicates.
equals method: Private data is accessible by any instance of that class,
even if one instance of class A is accessing the private members of another instance of A.
It's important to remember that that access modifiers (private, protected, public)
are controlling class access, not instance access.Board class, but also caching in Node inner class.
It is a crucial performance optimization because the points encountered while exploring the first subtree may enable pruning of the second subtree. For typical inputs, choosing the direction that goes toward the query point makes it more likely that we will encounter points close to the query point.
Use a structure (inner class) or member variables to save minimum distance to reduce the # of calls of distanceTo.
-1 if unvisited to save visited arrayduring second BFS, if vertex v is visited, calculate distA[v] + distB[v] to maintain minimum length.
check multi-roots: more than one vertices' outdegree is 0
Another angel of topological order: Kahn's algorithm basically looks for the nodes that do not have any incoming edges, or have indegree = 0, and then removes it's outgoing edges, making it's outdegree also equal to 0. Here's the algorithm step by step:
Find a vertex that has indegree = 0 (no incoming edges)
So, given the graph below, the topological order can be from Left to Right, from Top to bottom

energy[][] variable, compute each node at the first time and when update needed.Color type to energy function, avoid redundant calls to the get() method in PicturegetAllValidWords can be called multiple times, so do not modify your Trie to avoid duplicates during one call (Or you can reverse your modification after one call). Avoid similar code below.if (sb.length() >= 3 && next.isTernimal) {
set.add(sb.toString());
x.isTernimal = false; // but you can do this when Leetcoding
}
CircularSuffix inner class to represent a suffix string, which uses a reference pointing to the only one common input string outside the class.inverseTransform method.next array is the same with the way we calculate aux array in the algorithm.$ claude mcp add Princeton-algs4 \
-- python -m otcore.mcp_server <graph>