Leetcode-198-house robber

Source: Internet
Author: User

House robber

You is a professional robber planning to rob houses along a street. Each house have a certain amount of money stashed, the only constraint stopping all from robbing each of the them are that Adjac ENT houses have security system connected and it would automatically contact the police if the adjacent houses were bro Keninto on the same night.

Given a list of non-negative integers representing the amount of money in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.

Robbers enter the house theft, and can not enter two adjacent room theft, for maximum benefit.

That is, in an array to select a number of numbers, the selected number can not be adjacent, to find their maximum and.



Dynamic planning.

Max[i] is the maximum profit for the house to the first room.

Max[i] = max (Max[i-2] + nums[i], max[i-1])


Class Solution {public:    int Rob (vector<int>& nums) {        int n = nums.size ();        Vector<int>max (n,0);        if (n = = 0) return 0;        if (n = = 1) return nums[0];        Max[0] = nums[0];        MAX[1] = max (max[0],nums[1]);        for (int i = 2;i < n;i++) {             Max[i] = MAX (Max[i-2] + nums[i],max[i-1]);        }        return max[n-1];}    ;


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode-198-house robber

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.