Archive
Design Patterns
Design Patterns Creational Patterns How to create objects Factory Pattern When? Tight coupling between the client code and the concreate classes of a particular type.
Adhoc Problems
Decrease subarray by 1, min operations to make it 0 Link: leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array int minNumberOperations(vector<int>& arr) { // rephrase // invert the problem, in one operation, can dec subarray by 1, convert to 0 // greedy, s...
Prefix and Suffix Aggregates
Minimum range to sort to get a fully sorted array in O(n) and O(1) Link: leetcode.com/problems/shortest-unsorted-continuous-subarray/ Note that you can also use the “boundaries” approach here.
Tree Construction
Cartesian Trees It’s just a binary tree where each node is greater/smaller (based on value or some derived priority) than all nodes in its left subtree and right subtree.
Sliding Window
Count Subarrays with Fixed Bounds Quite an elegant solution once you realise the observation.
Dynamic Programming ( Classics )
Subarray sums Max/min sum subarray ( trivial ) int maxSubArray(vector<int>& nums) { int curs = nums[0], mx = nums[0]; for (int i = 1; i < (int)nums.size(); i++) { curs = max(nums[i], curs + nums[i]); mx = max(curs, mx); } return mx; } Max/min sum circular subarray ( complement of min tr...
Just enough CMake to get started
Aim is to scaffold a C++ project that uses Raylib and can be built with CMake.
Monotonic Constructs
Monotonic Deque Use: Sliding window max/min in O(1) per step Invariant Elements in the deque are monotonic (duplicates allowed) Store up to k elements in the deque for window size k New elements go to back; expired elements go out from front Idea Maximum in window = keep the largest elements in the ...
Model Context Protocol
Model Context Protocol These are basically glorified wrappers with the “function documentation” acting as a prompt for the model to use.