📜  算法|杂项|问题11

📅  最后修改于: 2021-06-28 21:38:59             🧑  作者: Mango

在一个村庄里,人们在路的同一侧建造房屋。一个小偷计划抢劫这个村庄。他希望获得最大的金钱,而又不会陷入任何风险。通过某种方式,村民知道邻居的房屋是否被洗劫一空,因此他们变得机警。因此,小偷不能抢劫连续的两所房子。假设小偷知道每所房子中存放的钱数,并且道路是直的且没有转弯,那是解决此问题的最有效的算法策略?
(A)蛮力
(B)动态编程
(C)回溯
(D)分而治之答案: (B)
解释:

If we take a closer look, the problem boils down to:
Given an array with some finite size where each element represents 
a positive number, find the maximum sum such that no two elements 
are adjacent.
Dynamic Programming is the efficient technique to solve this. 
The algorithm can be given as follows:
Maintain an auxiliary array loot.
loot[0] = arr[0]
loot[1] = arr[1]
loot[i] = max(loot[i - 1], loot[i - 2] + arr[i]),  2 <= i < n
loot[n - 1] gives the maximum amount of money the thief can take away.

这个问题的测验