leetcode 198-House Robber

来源:互联网 发布:网络机顶盒能看外国台 编辑:程序博客网 时间:2024/05/29 03:56

难度:easy

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.  打家劫舍,在不能同时打劫挨着的两家的情况下,求能偷到的最大金额。实质就是在一列数组中取出一个  或者多个不相邻的数,使其和最大,用到dp(动态规划,dynamic    programming).动态规划算法通常基于 一个递推公式及一个或多个初始状态,当前子问题的解将有上一次子问题的解推出。最重要是根据最初的数据找到递推公式。

           2.  此题中f[i]表示到第i家能偷窃到的最大钱数,f[0]=nums[0], f[1]=max(nums[0],nums[1]),                                f[2]=max(f[0]+nums[2].

           3.  递推公式是:f[i]=max(f[i-2]+nums[i],f[i-1])

           4.  写的不够简略





原创粉丝点击