Midterm2PracticeProblemsHints_Apr2018.pdf

# For example if santa chooses two gifts such that w 1

• Test Prep
• 5
• 100% (1) 1 out of 1 people found this document helpful

This preview shows pages 4–5. Sign up to view the full content.

For example, if Santa chooses two gifts such that w 1 = 3, u 1 = 4 and w 2 = 2, u 2 = 2 then the total weight of this set of gifts is 5 pounds and the total utility of this set of gifts is 8. All numbers mentioned are positive integers and for each gift i, w i < c . Design a dynamic programming algorithm that lets Santa maximize the util- ity of the set of gifts he packs in his sleigh without exceeding its capacity c . The inputs to your algorithm are c , n and a set of w i and u i pairs. The output of your algorithm should be the maximum utility value. Hint: This is similar to knapsack problem without repetition. Instead of adding the values, you need to multiply the utilities. Let M ( x, k ) be the maximal achievable utility if the gifts are drawn from 1 through k (where k n ) and weight at most x pounds. What subproblems do you need? How can you use these subproblems? 9. A palindrome is a sequence of characters that is the same if it is reversed. For example, ‘anna’ is a palindrome. Suppose that you are given a sequence of characters, and wish to determine whether it is made of one of more palin- dromes placed next to each other. For example, ‘dadseesanna’ is made of the palindromes ‘dad’, ‘sees’, and ‘anna’, placed next to each other. Design a dynamic programming algorithm to solve this problem. Hint: First write a function to determine if a string is a palindrome. Then borrow ideas from the in-class exercise where we determined if a sequence of characters could 4

This preview has intentionally blurred sections. Sign up to view the full version.

be broken into words. Hint: This is almost exactly like the in-class exercise mentioned. The only difference is that instead of having a dictionary to check if a string is a word, you need to check if the string is a palindrome. Write a function to do this. 10. Consider the extensible array from class. In this version of the problem, you need to pay for writing/copying an element, but you do not need to pay for allocating new memory. However, whenever you allocate new space and copy the elements over, you need to pay to release the old memory. The cost is \$1 per unit of memory that is released, and \$1 for each write/copy. Perform an amortized analysis for this problem. Hint: The total cost per operation, on average, is \$5. When it becomes time to double an array, the cost for that doubling will be \$k for copying the old elements and \$k for releasing the old memory, for a total of \$2k. The last doubling occured when the array had length k /2, so since then we have added k /2 elements. Thus, there are k /2 operations to spread this cost over. 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern