198. House Robber

来源:互联网 发布:淘宝推广有哪些渠道 编辑:程序博客网 时间:2024/05/21 14:47

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.

solution:

class Solution {public:    int rob(vector<int>& nums) {        int sz = nums.size();        if(sz == 0) return 0;        if(sz == 1) return nums[0];        int a = nums[0];        int b = 0;        int max_tmp = a;        for(int i = 1; i<sz; i++){            a = b + nums[i];            b = max_tmp;            max_tmp = max(a,b);        }        return  max_tmp;    }};
心得:动态规划

运行速度:快

0 0
原创粉丝点击