The idea is to determine the minimum number of coins for an amount n by trying out each of the k different coins as the first coin. Once a coin is chosen, say the one with value d[i], then we recursively find the optimum number of coins for the smaller amount n-d[i]. Of course, we want to use dynamic programming, not recursion, so instead of making recursive calls we store optimal values in an array. Here's the program:
CoinOpt(n,d[1..n])):
array opt[1..n];
opt[1] = 1; //
We know that we have a one cent coin.
for a = 2 to n do
// Ignore any values where n-d[i]
is negative.
opt[a] = 1 + MIN(i=1..k) of opt(n-d[i]);
return opt[n];
end.
The running time is obviously O(nk) because there are two loops (the amount-loop and the minimization loop), one nested within the other. The outer loop has n steps and the inner one has k steps, so the total running time is O(nk).
The solution here is much like that of the coin problem. The idea is to try each of the strings as the end-part of string D. Once a string is chosen, say one with length j, then we recursively find the optimum coding for the smaller string D[1..n-j]. Of course, we want to use dynamic programming, not recursion, so we store the optimal values in an array. Here's the program:
CompressionOpt(D[1..n],string[1..m,1,,k]): // D is the data string; string represents the m text strings used for // coding, each of length at most k. array opt[1..n]; // opt[i] will hold best code-size for D[1..i] for i = 1 to n do Determine which strings s appear at the end of D[1..i]; // This takes O(mk) time. opt[i] = 1 + MIN{over all such strings s) of opt(i-|s|); return opt[n].Since the program consists of simple nested loops, it's obvious that it takes time O(nmk).
If we put all the names in a single array and use binary search then every access takes time proportional to (log 10000) or about 13.29. If we put the 4000 good customers in an array by themselves then 60% of the time our search will take time (log 4000) and 40% of the time our search will take time (log 4000 + log 6000). The total expected time is thus: (log 4000) + 0.4 (log 6000) or about 11.97 + 5.02 = 16.99. Thus the first option (using a single list) gives better expected performance.
If we use linear search with unsorted arrays instead of binary search then the time for the first option is proportional to 5000 (i.e., we expect to search about half the list before we find the item we're looking for). For the second option, a good customer will be found in time proportional to 2000 (half the list of 4000) while other customers will take time 4000 (time to look through entire list of good customers) + 3000 (half the list of other customers). Thus the time using the second strategy will be proportional to 0.6(2000) + 0.4(7000) = 4000. Thus when using linear search on unsorted lists the 2-list strategy is better.