zoukankan      html  css  js  c++  java
  • House Robbers. 198 & 213

    198. House Robbers I:

    问题描述:

    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.

    Example 1:

    Input: [1,2,3,1]
    Output: 4
    Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
                 Total amount you can rob = 1 + 3 = 4.

    Example 2:

    Input: [2,7,9,3,1]
    Output: 12
    Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
                 Total amount you can rob = 2 + 9 + 1 = 12.

    思路:

    用dp来做,

    动态转移方程

    dp[0] = 0

    dp[1] = nums[0]

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

    由于dp[i]只与dp[i-1]和dp[i-2]有关,我们也可以用两个指针来代替

    注意边界情况:数组为空

    代码:

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

    ----------------------------我是下一道题分割线----------------------------------

    213: House RobberII

    问题描述:

    You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, 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.

    Example 1:

    Input: [2,3,2]
    Output: 3
    Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
                 because they are adjacent houses.
    

    Example 2:

    Input: [1,2,3,1]
    Output: 4
    Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
                 Total amount you can rob = 1 + 3 = 4.

    思路:

    这里的数组是个循环数组,首尾是相连的,我们可以用枚举方法来举出可能的非循环数组

    不包括第一个元素和不包括第二个元素

    需要注意边界情况:数组为空或数组只有一个元素。

    代码:

    class Solution {
    public:
        int rob(vector<int>& nums) {
            if(nums.size() == 0)
                return 0;
            if(nums.size() == 1)
                return nums[0];
            return max(robber(nums, 0, nums.size()-1), robber(nums, 1, nums.size()));
        }
    private:
        int robber(vector<int>& nums, int left, int right){
            
            int rob = nums[left];
            int nRob = 0;
            for(int i = left + 1; i < right; i++){
                int temp = max(rob, nRob);
                rob = nRob + nums[i];
                nRob = temp;
            }
            return max(rob, nRob);
        }
    };
  • 相关阅读:
    Javascript 获得数组中相同或不同的数组元素   
    Java IO流 FileOutputStream、FileInputStream的用法   
    你的项目中使用过哪些JSTL标签?
    JavaWeb代码复用
    软件质量保证体系是什么
    二叉树的递归遍历框架:
    二叉树节点的定义框架:
    SQL 2012 Always On 为 MSCRMSqlClrLogin SQL 登录名创建非对称密钥时报语法错误
    linux命令指usermod(管理用户以及权限的命令)
    Dynamics CRM 2013 SP1 升级到Dynamics CRM 2015
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9127834.html
Copyright © 2011-2022 走看看