1. 程式人生 > >動態規劃解決leetcode中的House Robber問題

動態規劃解決leetcode中的House Robber問題

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into
on the same night. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

也就是給一個數組,每個陣列的元素代表了這個房間中的金額,但是兩個相鄰的兩個房間不能同時搶劫。問題是強盜最多能搶劫多少金額?

假如陣列是:
1,3,5,7,9
也就是第一間房有1百萬,第二間房有300萬。。。

動態規劃的思想就是將大問題拆解成小問題,然後儲存中間計算的值。這樣減少運算的複雜程度。

首先我定義兩個變數
pre_one:代表陣列中當前值所在位置前半部分序列的最優解
pre_two:代表陣列中當前值所在位置的前一個值的前半部分序列的最優解

比如說小偷現在走到了有700萬的那間房,他已經知道了1,3,5這三個房間的最優解,也就是pre_one。
已經知道了1,3這兩個房間的最優解,也就是pre_two。

到底要不要搶劫這間700萬的房呢?有兩個選擇:

  1. 如果搶劫該房間,那麼上一間房間就不能搶劫,這樣pre_two就派上了用場。
  2. 如果不搶劫該房間,那麼pre_one就派上了用場。
    最終的決定的依據就是判斷這兩種抉擇孰大孰小。

程式碼如下:

class Solution {
public
: int rob(vector<int>& nums) { // 儲存當前值的之前序列的最優解 int pre_one = 0; // 儲存當前值的前一個值的之前序列的最優解 int pre_two = 0; // 包含當前值的之前序列的最優解 int rob = 0; int len = nums.size(); for(int i=0; i<len; i++) { rob = max(pre_one, pre_two + nums[i]); pre_two = pre_one; pre_one = rob; } return rob; } };