Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Houses hold [16, 29, 33, 40, 3, 32, 33, 13] units of cash. You cannot rob two adjacent houses. What is the maximum amount you can rob?
- A. 85
- B. 114
- C. 119
- D. 113
Correct answer: B. 114
dp[i] = max(dp[i−1], dp[i−2]+a[i]) → 114. Taking every other house (85) is not always optimal.
More Coding Interview Problems questions
- How many subsets (by index, so equal values at different positions count separately) of [8, 7, 8, 6, 9, 3, 6, 1, 2] sum to exactly 16?
- What is the length of the longest palindromic subsequence (not substring) of "ADCBEACD"?
- Houses hold [15, 35, 2, 19, 25, 31, 12, 36] units of cash. You cannot rob two adjacent houses. What is the maximum amount you can rob?
- How many subsets (by index, so equal values at different positions count separately) of [4, 8, 1, 1, 5, 5] sum to exactly 8?
- What is the length of the longest palindromic subsequence (not substring) of "BDDCCBDB"?
- Houses hold [40, 38, 22, 30, 6, 2, 31, 18] units of cash. You cannot rob two adjacent houses. What is the maximum amount you can rob?