Python Algorithm Features - JangoBoogaloo/LeetCodeExcercise GitHub Wiki

bisect

Instead of manually write binary search. Use bisect_left or bisect_right

accumulate (Prefix Ops: +, *, max ....)

    print(list(accumulate([1, 1, 1, 1, 1])))

    1, 2, 3, 4, 5

cache (Nice trick for DP)

@cache
def factorial(n):
    return n * factorial(n-1) if n else 1

>>> factorial(10)      # no previously cached result, makes 11 recursive calls
3628800
>>> factorial(5)       # just looks up cached value result
120
>>> factorial(12)      # makes two new recursive calls, the other 10 are cached
479001600

⚠️ Manually DP caching might still be faster than decorator