Teaching Kids Programming: Videos on Data Structures and Algorithms
Eric wants to build a Rectangle Fence against an old wall using length of bricks (all 32), see below:
-------------------------- // old wall
| |
| |
L____J // new wall
Eric wants to maximize the area of the fence. What is the height and width?
Compute the Max Fence Area via Bruteforce Algorithm
Since the unit is integer – we can bruteforce all possibilities of either H or W. The domain space is limited. We can either list the values in a table or use computer program to search for the maximal area:
def getMaxAreaOfFence():
ans = 0
maxh = 0
for h in range(1, 16):
w = 32 - h - h
if w * h > ans:
ans = w * h
maxH = h
return ans, h, 32 - h - h
Maximal Area by Solving Parabola Quadratic Equation
Mathematically speak, the area function f(w, h) aka
which can then be rewritten as a quadratic equation
i.e.
.
The symmetric axis for this function is
which is (h=8), where this function has a maximal value. The area is 8*16=128
–EOF (The Ultimate Computing & Technology Blog) —
445 wordsLast Post: Teaching Kids Programming - Number of Unique Email Addresses
Next Post: Teaching Kids Programming - Compute the Maximal Perimeter by Forming a Rectangle from N squares