Grokking Algorithms


Is it possible that the solution will require



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə89/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   85   86   87   88   89   90   91   92   ...   122
grokking-algorithms-illustrated-programmers-curious

Is it possible that the solution will require
more than two sub-knapsacks?
It’s possible that the best solution involves stealing more than two items. 
The way the algorithm is set up, you’re combining two knapsacks at 
most—you’ll never have more than two sub-knapsacks. But it’s possible 
for those sub-knapsacks to have their own sub-knapsacks.


178
Chapter 9
 
 
I
 
 
Dynamic programming
Is it possible that the best solution doesn’t
fill the knapsack completely?
Yes. Suppose you could also steal a diamond.
This is a big diamond: it weighs 3.5 pounds. It’s worth a million 
dollars, way more than anything else. You should definitely steal it! 
But there’s half a pound of space left, and nothing will fit in
that space.
EXERCISE
9.2
Suppose you’re going camping. You have a knapsack that will hold
6 lb, and you can take the following items. Each has a value, and the 
higher the value, the more important the item is:
• Water, 3 lb, 10
• Book, 1 lb, 3
• Food, 2 lb, 9
• Jacket, 2 lb, 5
• Camera, 1 lb, 6
What’s the optimal set of items to take on your camping trip?
Longest common substring
You’ve seen one dynamic programming problem so far. What are
the takeaways?
• Dynamic programming is useful 
when you’re trying to optimize 
something given a constraint
. In the knapsack problem, you had to 
maximize the value of the goods you stole, constrained by the size of 
the knapsack. 
• You can use dynamic programming when the problem can be broken 
into discrete subproblems, and they don’t depend on each other.


179
Longest common substring
It can be hard to come up with a dynamic-programming solution. That’s 
what we’ll focus on in this section. Some general tips follow:
• Every dynamic-programming solution involves a grid.
• The values in the cells are usually what you’re trying to optimize.
For the knapsack problem, the values were the value of the goods.
Each cell is a subproblem, so think about how you can divide
your problem into subproblems. That will help you figure out what 
the axes are.
Let’s look at another example. Suppose you run dictionary.com. 
Someone types in a word, and you give them the definition.
But if someone misspells a word, you want to be able to guess 
what word they meant. Alex is searching for
 fish
, but he 
accidentally put in 
hish
. That’s not a word in your dictionary,
but you have a list of words that are similar.
(This is a toy example, so you’ll limit your list to two words. In reality, 
this list would probably be thousands of words.) 
Alex typed
 hish
. Which word did Alex mean to type: 
fish
or 
vista


Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   85   86   87   88   89   90   91   92   ...   122




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin