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);
        }
    };
  • 相关阅读:
    Android 自定义RadioButton的样式
    利用ListView批量删除item
    android中checkbox自定义样式
    Android 限定符
    Android Fragment的用法(二)
    Android Fragment的用法(一)
    Android 自定义控件
    Android 外部启动activity,自定义action,action常量大全
    Android之编写测试用例
    Android 定制自己的日志工具
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9127834.html