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

PPT
Profiling and optimization
PDF
Data structures
PDF
PPTX
DA_02_algorithms.pptx
PDF
PPT
19 algorithms-and-complexity-110627100203-phpapp02
PDF
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
PDF
Algorithms
Profiling and optimization
Data structures
DA_02_algorithms.pptx
19 algorithms-and-complexity-110627100203-phpapp02
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
Algorithms

Similar to funwithalgorithms.pptx (20)

PDF
Algorithm.pdf
PDF
Algorithms notes for professionals
PDF
Algorithms notesforprofessionals
PDF
Algorithms notes for professionals
PPTX
Top 10 most used algorithm ppt template.pptx
PPTX
19. algorithms and-complexity
PPTX
Cracking the Facebook Coding Interview.pptx
PPTX
1_Asymptotic_Notation_pptx.pptx
DOCX
CS3491-AI and ML lab manual cs3491 r2021
PDF
Sienna 2 analysis
PDF
AI_Lab_File()[1]sachin_final (1).pdf
PPTX
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
PPTX
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
PPTX
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPTX
Algorithms Exam Help
PPT
Chap11alg
PPT
Chap11alg
PPTX
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
PDF
Analysis and design of algorithms part 4
PDF
Unit-1 DAA_Notes.pdf
Algorithm.pdf
Algorithms notes for professionals
Algorithms notesforprofessionals
Algorithms notes for professionals
Top 10 most used algorithm ppt template.pptx
19. algorithms and-complexity
Cracking the Facebook Coding Interview.pptx
1_Asymptotic_Notation_pptx.pptx
CS3491-AI and ML lab manual cs3491 r2021
Sienna 2 analysis
AI_Lab_File()[1]sachin_final (1).pdf
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
Algorithms Exam Help
Chap11alg
Chap11alg
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
Analysis and design of algorithms part 4
Unit-1 DAA_Notes.pdf
Ad

More from Tess Ferrandez (15)

PPTX
Debugging .NET apps
PPTX
CSI .net core - debugging .net applications
PPTX
Debugging performance issues, memory issues and crashes in .net applications rev
PPTX
Common asp.net production issues rev
PPTX
Perf by design
PPT
Fun421 stephens
PPTX
C# to python
PPTX
Facenet - Paper Review
PPTX
AI and Ethics - We are the guardians of our future
PPTX
Deep learning and computer vision
PPTX
A practical guide to deep learning
PDF
Notes from Coursera Deep Learning courses by Andrew Ng
PPTX
A developers guide to machine learning
PPTX
My bot has a personality disorder
PPTX
.Net debugging 2017
Debugging .NET apps
CSI .net core - debugging .net applications
Debugging performance issues, memory issues and crashes in .net applications rev
Common asp.net production issues rev
Perf by design
Fun421 stephens
C# to python
Facenet - Paper Review
AI and Ethics - We are the guardians of our future
Deep learning and computer vision
A practical guide to deep learning
Notes from Coursera Deep Learning courses by Andrew Ng
A developers guide to machine learning
My bot has a personality disorder
.Net debugging 2017
Ad

Recently uploaded (20)

PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
AI in Product Development-omnex systems
PPTX
L1 - Introduction to python Backend.pptx
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
medical staffing services at VALiNTRY
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
AI in Product Development-omnex systems
L1 - Introduction to python Backend.pptx
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
ISO 45001 Occupational Health and Safety Management System
Adobe Illustrator 28.6 Crack My Vision of Vector Design
ManageIQ - Sprint 268 Review - Slide Deck
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Nekopoi APK 2025 free lastest update
Upgrade and Innovation Strategies for SAP ERP Customers
medical staffing services at VALiNTRY
Odoo POS Development Services by CandidRoot Solutions
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo Companies in India – Driving Business Transformation.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf

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

  • #14: 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
  • #15: 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
  • #17: 10 trillion calculations
  • #18: Set the values and is_valid for koko eating bananas
  • #19: - Very rough calculation to compare algorithms - Considers worst case scenario - Use it to compare time and storage used for algorithms
  • #22: Set the values and is_valid for koko eating bananas
  • #27: 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)
  • #28: 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)
  • #31: Great for exploration Not good for shortest path – we need to find all paths
  • #33: Great for full exploration Great for shortest path
  • #34: McClane and Zeus (Bruce Willis and Samuel L Jackson)
  • #35: Great for full exploration Great for shortest path
  • #36: Great for full exploration Great for shortest path
  • #37: Great for full exploration Great for shortest path
  • #39: Sets Groups Islands Circle of friends
  • #40: Sets Groups Islands Circle of friends
  • #41: Great for full exploration Great for shortest path
  • #42: Great for full exploration Great for shortest path
  • #43: 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”
  • #48: Just adding the little @cache in here might save a few roundtrips to the database or what not
  • #57: A special case of dynamic programming
  • #64: A special case of dynamic programming
  • #65: 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
  • #66: A special case of dynamic programming