House Robber

来源:互联网 发布:钢铁力量狐狸升级数据 编辑:程序博客网 时间:2024/04/30 00:05

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 andit 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 tonightwithout alerting the police

class Solution {public:    int rob(vector<int> &num) {        int n = num.size();        if (n < 1)        {            return 0;        }        if (n == 1)        {            return num[0];        }        int buf[n];        int maxTable[n];        buf[0] = num[0];        maxTable[0] = num[0];        buf[1] = num[1];        maxTable[1] = max(buf[0], buf[1]);        for (int i = 2; i < n; i++)        {            buf[i] = maxTable[i-2] + num[i];            maxTable[i] = max(maxTable[i-1], buf[i]);        }        return maxTable[n-1];    }};

.

0 0
原创粉丝点击