https://en.wikipedia.org/wiki/Euclidean_algorithm
function gcd(a, b)
if b = 0
return a
else
return gcd(b, a mod b)top get smallest item e.g.
priority_queue<int,vector<int>,greater<>> pq {sticks.begin(), sticks.end()};class Solution {
public:
priority_queue<int> q;
int lastStoneWeight(vector<int>& stones) {
for (int i : stones){
q.push(i);
}
while(q.size() > 1){
int x = q.top();
q.pop();
int y = q.top();
q.pop();
if (x-y != 0){
q.push(x-y);
}
}
return q.size() == 1 ? q.top() : 0;
}
};a⊕b⊕a=(a⊕a)⊕b=0⊕b=b
find items <= something https://en.cppreference.com/w/cpp/algorithm/lower_bound
grokking the system design interview https://www.educative.io/courses/grokking-the-system-design-interview high scalability blog