zoukankan      html  css  js  c++  java
  • House Robber && House Robber II

    题目: 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.

    思路:首先想到用dp做。用dp做的时候脑子里想了这些:

    1. state:我们用f(i) 表示在第i户,我们能抢到的最多的钱。

    2. function:f(i) = max(f(i - 2) + Mi,  f(i - 1)). 原因是,如果连续强两个,人家就报警了~

    3. initialize: f(0) = num[0] , f(1) = max(num[0], num[1]); 在这里我出了bug,没有考虑对于f(1)取最大值,无脑初始化了。

    4. return f(1)

     1 public int rob(int[] num) {
     2         if (num == null || num.length == 0) {
     3             return 0;
     4         }
     5         if (num.length == 1) {
     6             return num[0];
     7         }
     8         if (num.length == 2) {
     9             return Math.max(num[0], num[1]);
    10         }
    11         int[] sum = new int[2];
    12         sum[0] = num[0];
    13         sum[1] = Math.max(num[1], num[0]);
    14         for (int i = 2; i < num.length; i++) {
    15             int temp = Math.max(sum[0] + num[i], sum[1]);
    16             sum[0] = sum[1];
    17             sum[1] = temp;
    18         }
    19         return sum[1];
    20     }

    House Robber II

    题目:

    Note: This is an extension of House Robber.

    After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

    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.

    思路:

    这题是house robber的扩展,就是说房子围成一个圈,首位也是相邻的。假如有1->2->3->4->5。如果抢了1,就不能抢5。因为首尾相连。

    那我们既然已经解决了首位不相邻的问题,就相当于,我们已经解决了让一个小偷从0偷到nums.length - 1的问题。

    思考这道题的区别。区别就在于,如果偷了头一家,就别偷最后一家,如果没偷头一家,就可以偷最后一家。

    换句话说,我们需要知道小偷从0偷到nums.length - 2 和 从1偷到nums.length - 1的问题。 然后返回他两者中的max。

    代码如下,helper funciton就做了上题中rob所做的事。

     1 public int rob(int[] nums) {
     2         if (nums == null || nums.length == 0) {
     3             return 0;
     4         }
     5         if (nums.length == 1) {
     6             return nums[0];
     7         }
     8         int rob = helper(nums, 0, nums.length - 1);
     9         int notRob = helper(nums, 1, nums.length);
    10         return Math.max(rob, notRob);
    11     }
    12     
    13     private static int helper(int[] nums, int start, int end){
    14         if (start == end) {
    15             return nums[start];
    16         }
    17         int[] sum = new int[2];
    18         sum[0] = 0;
    19         sum[1] = 0;
    20         for(int i = start; i < end; i++) {
    21             int temp = Math.max(sum[0] + nums[i], sum[1]);
    22             sum[0] = sum[1];
    23             sum[1] = temp;
    24         }
    25         return sum[1];
    26     }
  • 相关阅读:
    一些数论公式
    一位ACMer过来人的心得
    hdu 2069 Coin Change (母函数)
    关于 A^x = A^(x % Phi(C) + Phi(C)) (mod C) 的若干证明
    upper_bound()与lower_bound()使用方法
    POJ 计算几何入门题目推荐
    图像识别C++读取bmp位图入门
    Centos7 上安装FastDFS
    java的IO包类分层结构
    ==和equels
  • 原文地址:https://www.cnblogs.com/gonuts/p/4395245.html
Copyright © 2011-2022 走看看