Dynamic Programming: Key Parts

Dynamic Programming: Key Parts

You're sitting at the public park on a beautiful summer day (as days usually are when you make them up in your head), minding your own business when out of nowhere a gigantic dog comes barreling towards you. No, really. This thing looks like a cross between an Irish wolfhound and a great white shark.

"Oh, don't worry," says the owner from too far away to save you. "Hercules is friendly, just a big old teddy bear."

Bears eat people sometimes. That's not comforting at all. Why would they think that's comforting?

In the end, though, Hercules just slobbers all over your face and book (hopefully it wasn't a signed first edition or anything). You may never get your money's worth on that book now, but good old Herc-y is friendly and safe, despite how terrifying he looked.

Just like your new best canine friend, dynamic programming might look intimidating at first glance. It also isn't really so bad once you get to know it (gross dog spit aside).

There are a couple of ways to solve a dynamic programming problem, meaning that you'll need to figure out which approach is the best choice for your problem. You could

  • break the problem down. 
  • define the substructure of an optimal solution.
  • generate a bottom up solution.

If that isn't your style, though, you could

  • solve the problem recursively (if possible).
  • memoize (store) intermediate solutions to a top-down solution.

Let's walk through how to decide which one to use. (Click on the buckets below for more.)

Dog treats sold separately.

When to Use Dynamic Programming

When you pay cash for something like a giant stuffed unicorn, unless you need to squirrel away quarters for laundry or you just really hate the design of nickels, there isn't an optimal combination...

Break the Problem Down

Somewhere along historic Route 66 (probably in Texas), you'll find The Big Texan. Despite the name, it isn't a person, it's a restaurant. The biggest thing on the menu is a 72 oz. steak, which you...

Bottom-Up Solutions

There are people out there who like to run long distances for fun. For fun. It's true. Anyway, those running-type people start with one step. One step leads to another and another…and another. Th...

Recursive, Memoized, Top-Down Solutions

Top-down solutions start with one big problem and work down to the little problems that help solve that one big problem. It's like when you accidentally flush your family's beloved pet salamander d...