SlideShare uma empresa Scribd logo
1 de 62
TESS FERRANDEZ
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 ]
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!!!
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)
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
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
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
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
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
TESS FERRANDEZ

Mais conteúdo relacionado

Semelhante a 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 NYCPythonakaptur
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcionaltdc-globalcode
 
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 programmingLukasz 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.pdfkesav24
 
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 212Mahmoud Samir Fayed
 
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~
PostgreSQL10の新機能 ~ロジカルレプリケーションを中心に~Atsushi Torikoshi
 
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 212Mahmoud Samir Fayed
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
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 185Mahmoud Samir Fayed
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)riue
 

Semelhante a 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
 

Mais de Tess Ferrandez

CSI .net core - debugging .net applications
CSI .net core - debugging .net applicationsCSI .net core - debugging .net applications
CSI .net core - debugging .net applicationsTess 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 revTess Ferrandez
 
Common asp.net production issues rev
Common asp.net production issues revCommon asp.net production issues rev
Common asp.net production issues revTess Ferrandez
 
Facenet - Paper Review
Facenet - Paper ReviewFacenet - Paper Review
Facenet - Paper ReviewTess 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 futureTess Ferrandez
 
Deep learning and computer vision
Deep learning and computer visionDeep learning and computer vision
Deep learning and computer visionTess Ferrandez
 
A practical guide to deep learning
A practical guide to deep learningA practical guide to deep learning
A practical guide to deep learningTess 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 NgTess Ferrandez
 
A developers guide to machine learning
A developers guide to machine learningA developers guide to machine learning
A developers guide to machine learningTess Ferrandez
 
My bot has a personality disorder
My bot has a personality disorderMy bot has a personality disorder
My bot has a personality disorderTess Ferrandez
 

Mais de 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
 

Último

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Último (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

funwithalgorithms.pptx

  • 2.
  • 3.
  • 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 ]
  • 11.
  • 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!!!
  • 14.
  • 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)
  • 19.
  • 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)
  • 23.
  • 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
  • 28.
  • 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
  • 30.
  • 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
  • 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
  • 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
  • 58.
  • 59.
  • 60.
  • 61.

Notas do Editor

  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