Leetcode (H)

来源:互联网 发布:qq邮箱有没有mac版本 编辑:程序博客网 时间:2024/05/16 19:22



House Robber

 Total Accepted: 3183 Total Submissions: 11595My Submissions

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.










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
















0 0
原创粉丝点击