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:
1 2 3 4 5 6 7 8 9 | 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 |
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
The symmetric axis for this function is
–EOF (The Ultimate Computing & Technology Blog) —
Last 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