SlideShare a Scribd company logo
TESS FERRANDEZ
funwithalgorithms.pptx
funwithalgorithms.pptx
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
I’m thinking of
a number 1-100
1-100
Hmm… 50?
HIGHER
51-100 .. 75?
76-100 .. 88?
76-87 .. 81?
82-87 .. 84?
85-87 .. 86?
85-85 .. 85!!!
higher
lower
higher
higher
lower
BINARY SEARCH
1, 2, 3, 4, 5, 6, 7, 8, 9,
...,85
O(n)
50, 75, 88, 81, 84, 86,
85
log2(100) = 6.64
2^6 = 64
2^7 = 128
O(log n)
log2(100) = 7
log2(1 000 000) = 20
log2(1 000 000 000) = 29
1, 2, 3, 4, 5, ..., 1 000
000
Find 7 – in a sorted list
0 1 2 3 4 5 6 7
8
[ 1 3 7 8 10 11 15
16 17 ]
funwithalgorithms.pptx
8:00
3 6 7 11
2 bph 2 + 3 + 4 + 6
= 15h
3 BPH 1 + 2 + 3 + 4
= 10h
4 BPH 1 + 2 + 2 + 3
1 bph 3 + 6 + 7 +
11 = 27h
10 000 piles and 1 000 000 000 bananas in each!!!
funwithalgorithms.pptx
8:00
3 6 7 11
2 bph 2 + 3 + 4 + 6
= 15h …
11 BPH 1 + 1 + 1 + 1
1 bph 3 + 6 + 7 +
11 = 27h
P * M calculations
10 000 piles * 1 000 000 000 hours
= 10 000 000 000 000 calculations
M = Max
Bananas
per pile
P = Piles
Koko eating bananas
def get_lowest_speed(piles, hours):
low = 1
high = max(piles)
while low < high:
mid = low + (high - low) // 2
if sum([ceil(pile / speed) for pile in piles]) <= hours:
high = mid
else:
low = mid + 1
return low
O(p * log max(p))
10 000 * 29 vs
10 000 * 1 000 000 000
BIG O NOTATION
print(“fruit salad”)
fruits = [“apple”, “banana”, …, “orange”]
print(“step 1”)
…
print(“step 2”)
for fruit in fruits:
print(fruit)
for fruit1 in fruits:
for fruit2 in fruits:
print(fruit1, fruit2)
idx = bsearch(fruits, “banana”) fruits.sort()
O(1)
O(n) O(n^2)
O(log n) O(n * log n)
funwithalgorithms.pptx
Sleep sort
def print_number(number):
time.sleep(number)
print(number, end='')
def sleep_sort(numbers):
for number in numbers:
Thread(target=print_number, args=(number,)).start()
O(n)-ish
print(“fruit salad”)
fruits = [“apple”, “banana”, …, “orange”]
print(“step 1”)
…
print(“step 2”)
for fruit in fruits:
print(fruit)
for fruit1 in fruits:
for fruit2 in fruits:
print(fruit1, fruit2)
idx = bsearch(fruits, “banana”) fruits.sort() if “peach” in fruits:
print(“tropical”)
O(1) O(n) O(n^2)
O(log n) O(n * log n) O(n)
O(1)
GRAPHS
funwithalgorithms.pptx
tree binary search tree
trie / prefix tree linked list
roads = [
[“Oslo”, “Stockholm”],
[“Oslo”, “Copenhagen”],
[“Stockholm”, “Copenhagen”],
[“Stockholm”, “Helsinki”]]
OSLO
STOCKHOLM
Copenhagen
Helsinki
roads = [
[“Oslo”, “Stockholm”, 70],
[“Oslo”, “Copenhagen”, 50],
[“Stockholm”, “Copenhagen”, 30],
[“Stockholm”, “Helsinki”, 21]]
graph =
{
“Oslo”: (“Stockholm”, “Copenhagen”),
“Stockholm”: (“Oslo”, “Copenhagen”, “Helsinki”),
“Copenhagen”: (“Oslo”, “Stockholm”)
“Helsinki”: (“Stockholm”)
}
OSLO
STOCKHOLM
Copenhagen
Helsinki
graph =
{
“Oslo”: { “Stockholm” : 70, “Copenhagen” : 50 },
“Stockholm” : { “Oslo”: 70, “Copenhagen”: 30,
“Helsinki”: 21 },
“Copenhagen”: { “Oslo” : 50, “Stockholm”: 30,}
“Helsinki”: { “Stockholm”: 21 }
}
Depth First Search (DFS)
def dfs(graph, start, goal):
if start == goal:
return 0
visited = set([start])
stack = [(start, 0)]
while stack:
current, steps = stack.pop()
for neighbor in graph[current]:
if neighbor == goal:
return steps + 1
if neighbor not in visited:
visited.add(neighbor)
stack.append((neighbor, steps + 1))
return -1
funwithalgorithms.pptx
Breadth First search (BFS)
1 2
2
3
3
4
4
4
def bfs(graph, start, goal):
if start == goal:
return 0
visited = set([start])
queue = deque([(start, 0)])
while queue:
current, steps = queue.popleft()
for neighbor in graph[current]:
if neighbor == goal:
return steps + 1
if neighbor not in visited:
visited.add(neighbor)
queue.append((neighbor, steps + 1))
return -1
funwithalgorithms.pptx
Jugs
Jugs
(5, 0) (2, 3)
(2, 0) (0, 2)
(5, 2) (4, 3)
Jugs
(5, 0) (2, 3)
(2, 0) (0, 2)
(5, 2) (4, 3)
Breadth First Search (BFS)
Shortest/cheapest path
BFS + state in visited
(i.e. key or no key)
Dijkstra
A*
Bellman-ford (negative)
Course Schedule
Course Pre-req
1 0
2 0
3 1
3 2
Course Schedule – Topo sort
Course Pre-req
1 0
2 0
3 1
3 2
3 1 2 0
DYNAMIC
PROGRAMMING
Fibonacci
1, 1, 2, 3, 5, 8, 13
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
Fibonacci
Fibonacci
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)
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]
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
funwithalgorithms.pptx
funwithalgorithms.pptx
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
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
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
SLIDING WINDOW
Max sum subarray (of size k=4)
0 1 2 3 4 5 6 7
1 12 -5 -6 50 3 12 -1
1 + 12 - 5 - 6 = 2
12 - 5 - 6 + 50 = 51
-5 - 6 + 50 + 3 = 42
-6 + 50 + 3 + 12 = 59
50 + 3 + 12 - 1 = 64 O((n-k)*k)
Max sum subarray (of size k)
0 1 2 3 4 5 6 7
1 12 -5 -6 50 3 12 -1
1 + 12 - 5 - 6 = 2
2 - 1 + 50 = 51
51 - 12 + 3 = 42
42 - (-5) + 12 = 59
59 - (-6) + (-1) = 64 O(n)
1 0 1 2 1 1
7 5
0 0 0 2 0 1
0 5
Always happy: 1 + 1 + 1 + 7 = 10
Total Happy = 16 (10 + 6) O(n)
Grumpy Bookstore Owner
MAX:
0
2
6
3
MAX:
1
2
3
4
O(n)
Fruits into baskets
Container with most water
1 * 8 =
8
8 * 5 =
40
7 * 7 =
49
8 * 0 =
0
O(n)
MAX:
8
49
THE ALGORITHM
OF AN ALGORITH
A
L
G
O R
I
H
T M
WHAT’S NEXT
funwithalgorithms.pptx
funwithalgorithms.pptx
funwithalgorithms.pptx
funwithalgorithms.pptx
TESS FERRANDEZ

More Related Content

Similar to funwithalgorithms.pptx

Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
akaptur
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcional
tdc-globalcode
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
Roberto Pepato
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
Lukasz Dynowski
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
kesav24
 
The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212
Mahmoud Samir Fayed
 
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
Atsushi Torikoshi
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
NareshBopparathi1
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
Nattawut Phetmak
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Mozaic Works
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212
Mahmoud Samir Fayed
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
Arturo Herrero
 
Learning Lisp
Learning LispLearning Lisp
Learning Lisp
Alf Kristian Støyle
 
The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185
Mahmoud Samir Fayed
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
Simone Federici
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
Veera Pendyala
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 
Python.pdf
Python.pdfPython.pdf
Python.pdf
TanTran598844
 

Similar to funwithalgorithms.pptx (20)

Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcional
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212
 
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Learning Lisp
Learning LispLearning Lisp
Learning Lisp
 
The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185The Ring programming language version 1.5.4 book - Part 60 of 185
The Ring programming language version 1.5.4 book - Part 60 of 185
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
Python.pdf
Python.pdfPython.pdf
Python.pdf
 

More from Tess Ferrandez

Debugging .NET apps
Debugging .NET appsDebugging .NET apps
Debugging .NET apps
Tess Ferrandez
 
CSI .net core - debugging .net applications
CSI .net core - debugging .net applicationsCSI .net core - debugging .net applications
CSI .net core - debugging .net applications
Tess Ferrandez
 
Debugging performance issues, memory issues and crashes in .net applications rev
Debugging performance issues, memory issues and crashes in .net applications revDebugging performance issues, memory issues and crashes in .net applications rev
Debugging performance issues, memory issues and crashes in .net applications rev
Tess Ferrandez
 
Common asp.net production issues rev
Common asp.net production issues revCommon asp.net production issues rev
Common asp.net production issues rev
Tess Ferrandez
 
Perf by design
Perf by designPerf by design
Perf by design
Tess Ferrandez
 
Fun421 stephens
Fun421 stephensFun421 stephens
Fun421 stephens
Tess Ferrandez
 
C# to python
C# to pythonC# to python
C# to python
Tess Ferrandez
 
Facenet - Paper Review
Facenet - Paper ReviewFacenet - Paper Review
Facenet - Paper Review
Tess Ferrandez
 
AI and Ethics - We are the guardians of our future
AI and Ethics - We are the guardians of our futureAI and Ethics - We are the guardians of our future
AI and Ethics - We are the guardians of our future
Tess Ferrandez
 
Deep learning and computer vision
Deep learning and computer visionDeep learning and computer vision
Deep learning and computer vision
Tess Ferrandez
 
A practical guide to deep learning
A practical guide to deep learningA practical guide to deep learning
A practical guide to deep learning
Tess Ferrandez
 
Notes from Coursera Deep Learning courses by Andrew Ng
Notes from Coursera Deep Learning courses by Andrew NgNotes from Coursera Deep Learning courses by Andrew Ng
Notes from Coursera Deep Learning courses by Andrew Ng
Tess Ferrandez
 
A developers guide to machine learning
A developers guide to machine learningA developers guide to machine learning
A developers guide to machine learning
Tess Ferrandez
 
My bot has a personality disorder
My bot has a personality disorderMy bot has a personality disorder
My bot has a personality disorder
Tess Ferrandez
 
.Net debugging 2017
.Net debugging   2017.Net debugging   2017
.Net debugging 2017
Tess Ferrandez
 

More from Tess Ferrandez (15)

Debugging .NET apps
Debugging .NET appsDebugging .NET apps
Debugging .NET apps
 
CSI .net core - debugging .net applications
CSI .net core - debugging .net applicationsCSI .net core - debugging .net applications
CSI .net core - debugging .net applications
 
Debugging performance issues, memory issues and crashes in .net applications rev
Debugging performance issues, memory issues and crashes in .net applications revDebugging performance issues, memory issues and crashes in .net applications rev
Debugging performance issues, memory issues and crashes in .net applications rev
 
Common asp.net production issues rev
Common asp.net production issues revCommon asp.net production issues rev
Common asp.net production issues rev
 
Perf by design
Perf by designPerf by design
Perf by design
 
Fun421 stephens
Fun421 stephensFun421 stephens
Fun421 stephens
 
C# to python
C# to pythonC# to python
C# to python
 
Facenet - Paper Review
Facenet - Paper ReviewFacenet - Paper Review
Facenet - Paper Review
 
AI and Ethics - We are the guardians of our future
AI and Ethics - We are the guardians of our futureAI and Ethics - We are the guardians of our future
AI and Ethics - We are the guardians of our future
 
Deep learning and computer vision
Deep learning and computer visionDeep learning and computer vision
Deep learning and computer vision
 
A practical guide to deep learning
A practical guide to deep learningA practical guide to deep learning
A practical guide to deep learning
 
Notes from Coursera Deep Learning courses by Andrew Ng
Notes from Coursera Deep Learning courses by Andrew NgNotes from Coursera Deep Learning courses by Andrew Ng
Notes from Coursera Deep Learning courses by Andrew Ng
 
A developers guide to machine learning
A developers guide to machine learningA developers guide to machine learning
A developers guide to machine learning
 
My bot has a personality disorder
My bot has a personality disorderMy bot has a personality disorder
My bot has a personality disorder
 
.Net debugging 2017
.Net debugging   2017.Net debugging   2017
.Net debugging 2017
 

Recently uploaded

Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
kiara pandey
 
TEQnation 2024: Sustainable Software: May the Green Code Be with You
TEQnation 2024: Sustainable Software: May the Green Code Be with YouTEQnation 2024: Sustainable Software: May the Green Code Be with You
TEQnation 2024: Sustainable Software: May the Green Code Be with You
marcofolio
 
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
norina2645
 
03. Ruby Variables & Regex - Ruby Core Teaching
03. Ruby Variables & Regex - Ruby Core Teaching03. Ruby Variables & Regex - Ruby Core Teaching
03. Ruby Variables & Regex - Ruby Core Teaching
quanhoangd129
 
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
neshakor5152
 
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
singhlata50dh
 
ERP Software Solutions Provider in Coimbatore
ERP Software Solutions Provider in CoimbatoreERP Software Solutions Provider in Coimbatore
ERP Software Solutions Provider in Coimbatore
Nextskill Technologies
 
PathSpotter: Exploring Tested Paths to Discover Missing Tests (FSE 2024)
PathSpotter: Exploring Tested Paths to Discover Missing Tests (FSE 2024)PathSpotter: Exploring Tested Paths to Discover Missing Tests (FSE 2024)
PathSpotter: Exploring Tested Paths to Discover Missing Tests (FSE 2024)
andrehoraa
 
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
3610stuck
 
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
shanihomely
 
08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching
quanhoangd129
 
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
jealousviolet
 
07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching
quanhoangd129
 
How to Secure Your Kubernetes Software Supply Chain at Scale
How to Secure Your Kubernetes Software Supply Chain at ScaleHow to Secure Your Kubernetes Software Supply Chain at Scale
How to Secure Your Kubernetes Software Supply Chain at Scale
Anchore
 
Applitools Autonomous 2.0 Sneak Peek.pdf
Applitools Autonomous 2.0 Sneak Peek.pdfApplitools Autonomous 2.0 Sneak Peek.pdf
Applitools Autonomous 2.0 Sneak Peek.pdf
Applitools
 
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
dream girl
 
Test Polarity: Detecting Positive and Negative Tests (FSE 2024)
Test Polarity: Detecting Positive and Negative Tests (FSE 2024)Test Polarity: Detecting Positive and Negative Tests (FSE 2024)
Test Polarity: Detecting Positive and Negative Tests (FSE 2024)
andrehoraa
 
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
simran hot girls
 
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdfA Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
kalichargn70th171
 
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical SystemsInflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
Inflectra
 

Recently uploaded (20)

Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
Celebrity Girls Call Mumbai 9930687706 Unlimited Short Providing Girls Servic...
 
TEQnation 2024: Sustainable Software: May the Green Code Be with You
TEQnation 2024: Sustainable Software: May the Green Code Be with YouTEQnation 2024: Sustainable Software: May the Green Code Be with You
TEQnation 2024: Sustainable Software: May the Green Code Be with You
 
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
 
03. Ruby Variables & Regex - Ruby Core Teaching
03. Ruby Variables & Regex - Ruby Core Teaching03. Ruby Variables & Regex - Ruby Core Teaching
03. Ruby Variables & Regex - Ruby Core Teaching
 
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Mysore 000XX00000 Provide Best And Top Girl Service And No1 in City
 
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
High Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 ...
 
ERP Software Solutions Provider in Coimbatore
ERP Software Solutions Provider in CoimbatoreERP Software Solutions Provider in Coimbatore
ERP Software Solutions Provider in Coimbatore
 
PathSpotter: Exploring Tested Paths to Discover Missing Tests (FSE 2024)
PathSpotter: Exploring Tested Paths to Discover Missing Tests (FSE 2024)PathSpotter: Exploring Tested Paths to Discover Missing Tests (FSE 2024)
PathSpotter: Exploring Tested Paths to Discover Missing Tests (FSE 2024)
 
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
Mumbai Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service A...
 
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
 
08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching
 
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
VVIP Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No1 i...
 
07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching
 
How to Secure Your Kubernetes Software Supply Chain at Scale
How to Secure Your Kubernetes Software Supply Chain at ScaleHow to Secure Your Kubernetes Software Supply Chain at Scale
How to Secure Your Kubernetes Software Supply Chain at Scale
 
Applitools Autonomous 2.0 Sneak Peek.pdf
Applitools Autonomous 2.0 Sneak Peek.pdfApplitools Autonomous 2.0 Sneak Peek.pdf
Applitools Autonomous 2.0 Sneak Peek.pdf
 
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
Russian Girls Call Mumbai 🛵🚡9833363713 💃 Choose Best And Top Girl Service And...
 
Test Polarity: Detecting Positive and Negative Tests (FSE 2024)
Test Polarity: Detecting Positive and Negative Tests (FSE 2024)Test Polarity: Detecting Positive and Negative Tests (FSE 2024)
Test Polarity: Detecting Positive and Negative Tests (FSE 2024)
 
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
 
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdfA Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
 
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical SystemsInflectraCON 360: Risk-Based Testing for Mission Critical Systems
InflectraCON 360: Risk-Based Testing for Mission Critical Systems
 

funwithalgorithms.pptx

  • 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
  • 5. I’m thinking of a number 1-100 1-100 Hmm… 50? HIGHER 51-100 .. 75? 76-100 .. 88? 76-87 .. 81? 82-87 .. 84? 85-87 .. 86? 85-85 .. 85!!! higher lower higher higher lower
  • 7. 1, 2, 3, 4, 5, 6, 7, 8, 9, ...,85 O(n)
  • 8. 50, 75, 88, 81, 84, 86, 85 log2(100) = 6.64 2^6 = 64 2^7 = 128 O(log n)
  • 9. log2(100) = 7 log2(1 000 000) = 20 log2(1 000 000 000) = 29 1, 2, 3, 4, 5, ..., 1 000 000
  • 10. Find 7 – in a sorted list 0 1 2 3 4 5 6 7 8 [ 1 3 7 8 10 11 15 16 17 ]
  • 12. 8:00 3 6 7 11 2 bph 2 + 3 + 4 + 6 = 15h 3 BPH 1 + 2 + 3 + 4 = 10h 4 BPH 1 + 2 + 2 + 3 1 bph 3 + 6 + 7 + 11 = 27h
  • 13. 10 000 piles and 1 000 000 000 bananas in each!!!
  • 15. 8:00 3 6 7 11 2 bph 2 + 3 + 4 + 6 = 15h … 11 BPH 1 + 1 + 1 + 1 1 bph 3 + 6 + 7 + 11 = 27h P * M calculations 10 000 piles * 1 000 000 000 hours = 10 000 000 000 000 calculations M = Max Bananas per pile P = Piles
  • 16. Koko eating bananas def get_lowest_speed(piles, hours): low = 1 high = max(piles) while low < high: mid = low + (high - low) // 2 if sum([ceil(pile / speed) for pile in piles]) <= hours: high = mid else: low = mid + 1 return low O(p * log max(p)) 10 000 * 29 vs 10 000 * 1 000 000 000
  • 18. print(“fruit salad”) fruits = [“apple”, “banana”, …, “orange”] print(“step 1”) … print(“step 2”) for fruit in fruits: print(fruit) for fruit1 in fruits: for fruit2 in fruits: print(fruit1, fruit2) idx = bsearch(fruits, “banana”) fruits.sort() O(1) O(n) O(n^2) O(log n) O(n * log n)
  • 20. Sleep sort def print_number(number): time.sleep(number) print(number, end='') def sleep_sort(numbers): for number in numbers: Thread(target=print_number, args=(number,)).start() O(n)-ish
  • 21. print(“fruit salad”) fruits = [“apple”, “banana”, …, “orange”] print(“step 1”) … print(“step 2”) for fruit in fruits: print(fruit) for fruit1 in fruits: for fruit2 in fruits: print(fruit1, fruit2) idx = bsearch(fruits, “banana”) fruits.sort() if “peach” in fruits: print(“tropical”) O(1) O(n) O(n^2) O(log n) O(n * log n) O(n) O(1)
  • 24. tree binary search tree trie / prefix tree linked list
  • 25. roads = [ [“Oslo”, “Stockholm”], [“Oslo”, “Copenhagen”], [“Stockholm”, “Copenhagen”], [“Stockholm”, “Helsinki”]] OSLO STOCKHOLM Copenhagen Helsinki roads = [ [“Oslo”, “Stockholm”, 70], [“Oslo”, “Copenhagen”, 50], [“Stockholm”, “Copenhagen”, 30], [“Stockholm”, “Helsinki”, 21]]
  • 26. graph = { “Oslo”: (“Stockholm”, “Copenhagen”), “Stockholm”: (“Oslo”, “Copenhagen”, “Helsinki”), “Copenhagen”: (“Oslo”, “Stockholm”) “Helsinki”: (“Stockholm”) } OSLO STOCKHOLM Copenhagen Helsinki graph = { “Oslo”: { “Stockholm” : 70, “Copenhagen” : 50 }, “Stockholm” : { “Oslo”: 70, “Copenhagen”: 30, “Helsinki”: 21 }, “Copenhagen”: { “Oslo” : 50, “Stockholm”: 30,} “Helsinki”: { “Stockholm”: 21 } }
  • 27. Depth First Search (DFS) def dfs(graph, start, goal): if start == goal: return 0 visited = set([start]) stack = [(start, 0)] while stack: current, steps = stack.pop() for neighbor in graph[current]: if neighbor == goal: return steps + 1 if neighbor not in visited: visited.add(neighbor) stack.append((neighbor, steps + 1)) return -1
  • 29. Breadth First search (BFS) 1 2 2 3 3 4 4 4 def bfs(graph, start, goal): if start == goal: return 0 visited = set([start]) queue = deque([(start, 0)]) while queue: current, steps = queue.popleft() for neighbor in graph[current]: if neighbor == goal: return steps + 1 if neighbor not in visited: visited.add(neighbor) queue.append((neighbor, steps + 1)) return -1
  • 31. Jugs
  • 32. Jugs (5, 0) (2, 3) (2, 0) (0, 2) (5, 2) (4, 3)
  • 33. Jugs (5, 0) (2, 3) (2, 0) (0, 2) (5, 2) (4, 3)
  • 34. Breadth First Search (BFS) Shortest/cheapest path BFS + state in visited (i.e. key or no key) Dijkstra A* Bellman-ford (negative)
  • 36. Course Schedule – Topo sort Course Pre-req 1 0 2 0 3 1 3 2 3 1 2 0
  • 38. Fibonacci 1, 1, 2, 3, 5, 8, 13 def fib(n): if n <= 1: return n return fib(n - 1) + fib(n - 2)
  • 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
  • 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
  • 50. Max sum subarray (of size k=4) 0 1 2 3 4 5 6 7 1 12 -5 -6 50 3 12 -1 1 + 12 - 5 - 6 = 2 12 - 5 - 6 + 50 = 51 -5 - 6 + 50 + 3 = 42 -6 + 50 + 3 + 12 = 59 50 + 3 + 12 - 1 = 64 O((n-k)*k)
  • 51. Max sum subarray (of size k) 0 1 2 3 4 5 6 7 1 12 -5 -6 50 3 12 -1 1 + 12 - 5 - 6 = 2 2 - 1 + 50 = 51 51 - 12 + 3 = 42 42 - (-5) + 12 = 59 59 - (-6) + (-1) = 64 O(n)
  • 52. 1 0 1 2 1 1 7 5 0 0 0 2 0 1 0 5 Always happy: 1 + 1 + 1 + 7 = 10 Total Happy = 16 (10 + 6) O(n) Grumpy Bookstore Owner MAX: 0 2 6 3
  • 54. Container with most water 1 * 8 = 8 8 * 5 = 40 7 * 7 = 49 8 * 0 = 0 O(n) MAX: 8 49

Editor's Notes

  1. 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
  2. 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
  3. 10 trillion calculations
  4. Set the values and is_valid for koko eating bananas
  5. - Very rough calculation to compare algorithms - Considers worst case scenario - Use it to compare time and storage used for algorithms
  6. Set the values and is_valid for koko eating bananas
  7. 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)
  8. 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)
  9. Great for exploration Not good for shortest path – we need to find all paths
  10. Great for full exploration Great for shortest path
  11. McClane and Zeus (Bruce Willis and Samuel L Jackson)
  12. Great for full exploration Great for shortest path
  13. Great for full exploration Great for shortest path
  14. Great for full exploration Great for shortest path
  15. Sets Groups Islands Circle of friends
  16. Sets Groups Islands Circle of friends
  17. Great for full exploration Great for shortest path
  18. Great for full exploration Great for shortest path
  19. 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”
  20. Just adding the little @cache in here might save a few roundtrips to the database or what not
  21. A special case of dynamic programming
  22. A special case of dynamic programming
  23. 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
  24. A special case of dynamic programming