Bag of Tricks - pisanorg/w GitHub Wiki

A good programmer is one who can recognize patterns. Below are some of the patterns that appear frequently.

Arrays

Heap - Good for getting the nth largest number from an array. Building a binary heap takes O(n) time. Check out buildHeap and heapify from your favorite code sources.

Recursion - You are allowed to write the fibonacci function once recursively with O(2^n) run time. The next time you write it, you should use an array to store all the calculated values. This take O(n) space but now the run time is only O(n). If the function will get called multiple times, you can also use static function variables to implement memoization, so calculated values are remembered between different function calls. The third time you write this function, you should use 3 variables, making the space complexity O(1) and time complexity O(n). It is even possible to get O(1) time and space, but that requires some extra math knowledge, see gfg for details.