4. extract cells with a given color range
schedule jobs on azure batch in the
greenest and cheapest way
find the direction of moving objects
remove the background from a video
41. Memoization (Top down)
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
cache = {0: 0, 1: 1}
def fib(n):
if n not in cache:
cache[n] = fib(n-1) + fib(n-2)
return cache[n]
@cache
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
42. Tabulation (bottom up)
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
def fib(n):
fibs = [0, 1]
for i in range(2, n + 1):
fibs.append(fibs[i-1] + fibs[i-2])
return fibs[n]
43. 2 7 1 3 9
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
O(2 * n) so for n = 100 we have ~ 1.27 * 10 “operations”
n 32
House robber
44.
45.
46. Maxloot(0) = Loot[0] + Maxloot(2) or Maxloot(1)
Maxloot(1) = Loot[1] + Maxloot(3) or Maxloot(2)
Maxloot(2) = Loot[2] + Maxloot(4) or Maxloot(3)
Maxloot(3) = Loot[3] + Maxloot(5) or Maxloot(4)
Maxloot(4) = Loot[4] + Maxloot(6) or Maxloot(5)
0 1 2 3 4
2 7 1 3 9
47. Maxloot(0) = Loot[0] + Maxloot(2) or Maxloot(1)
Maxloot(1) = Loot[1] + Maxloot(3) or Maxloot(2)
Maxloot(2) = Loot[2] + Maxloot(4) or Maxloot(3)
Maxloot(3) = Loot[3] + Maxloot(5) or Maxloot(4)
Maxloot(4) = Loot[4] + Maxloot(6) or Maxloot(5)
0 1 2 3 4
2 7 1 3 9
48. def rob(loot: list[int]) -> int:
@cache
def max_loot(house_nr):
if house_nr > len(loot):
return 0
rob_current = loot[house_nr] + max_loot(house_nr + 2)
dont_rob_current = max_loot(house_nr + 1)
return max(rob_current, dont_rob_current)
return max_loot(0)
O(n) so for n = 100 we have 100 “operations”
Instead of 1.27 * 10^32
Examples look small and benign – but look at the Gianormous amount of piles and bananas – Koko must be very hungry
Sidenote – I always add an assert solution.eating_speed([3, 6, 7, 11], 8) == 4 for all test cases so I can experiment with solutions
Examples look small and benign – but look at the Gianormous amount of piles and bananas – Koko must be very hungry
Sidenote – I always add an assert solution.eating_speed([3, 6, 7, 11], 8) == 4 for all test cases so I can experiment with solutions
10 trillion calculations
Set the values and is_valid for koko eating bananas
- Very rough calculation to compare algorithms
- Considers worst case scenario
- Use it to compare time and storage used for algorithms
Set the values and is_valid for koko eating bananas
Facebook – undirected graph (mutual friends)
Twitter – directed graph, everyone follows Beyonce, but Beyonce doesn’t follow you
Cities and Roads – weighted graph – note the cycle between the first 3 cities
Tasks – weighted (nodes), and disjoint sets – could also have a flow ??? Not sure if it fits here
Matrix – neighbors are UDLR (if not blocked) + some are ok only if you have the key
Horse – neighbors are 2+1 away (add animation)
Special Graphs
Tree – doesn’t need to be binary – animate in mom2
A Tree is a DAG but not all DAGs are trees – animate in link between mom and 2nd grandma
Binary Search Tree – Very fast structure for sorted sets
Trie/Prefixtree (or suffix) – used for pattern matching – ex routes in APIs
Linked list – single next rather than multiple
Also others,
Minimum Spanning Tree (simplified tree with shortest paths)
Segment Tree (for segment queries ex. sum[a-f] – often in databases – heard its also used for shazam searches)
Great for exploration
Not good for shortest path – we need to find all paths
Great for full exploration
Great for shortest path
McClane and Zeus (Bruce Willis and Samuel L Jackson)
Great for full exploration
Great for shortest path
Great for full exploration
Great for shortest path
Great for full exploration
Great for shortest path
Sets
Groups
Islands
Circle of friends
Sets
Groups
Islands
Circle of friends
Great for full exploration
Great for shortest path
Great for full exploration
Great for shortest path
The idea of saving for example a visited set – so we don’t revisit nodes we already checked
saves us from infinite loops, but is also a type of “caching” – which brings us to “dynamic programming”
Just adding the little @cache in here might save a few roundtrips to the database or what not
A special case of dynamic programming
A special case of dynamic programming
ASK:
Uppercase/lowercase?
Negative numbers?
Sorted?
What do I actually need to calculate?
Memory/Time limits?
Have I seen similar questions before?
Don’t simulate the process or find all paths if you only need to find out the number of steps
Maybe we can just find a mathematical pattern
LIMIT:
A time of log n => binary search or divide and conquer
Very large sets mean we need to think more about time/space
GROK:
Draw the algorithm
Go through all the examples manually
Find patterns
Draw diagrams and arrows and whatever else you need
OMG!
Is “abba” an anagram of “baba”? => Sorted(“abba”) = SORTED(“BABA”)
Or same character counts
A recipe could be represented As a graph of tasks
MaxLoot[i] = max(Loot[i] + maxloot[I + 2], MaxLOOT[I + 1])
Is power of 2? => binary representation has one 1
Is palindrome => has only one char with odd count
REDUCE:
Find high/low limits
Design is-valid
Implement binary search
IMPLEMENT:
Consider data structures
Use templates / algos from internet
Start simple (maybe brute force) and optimize
ITS ONLY NOW THAT WE START IMPLEMENTING
Write lots of pseudo code
Optimize one piece at a time
TEST:
BE HAPPY:
MORE VARIATIONS:
Try variations
Limit yourself
Look at other peoples solutions