zoukankan      html  css  js  c++  java
  • 213. House Robber II Java Solutions

    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.

    Credits:
    Special thanks to @Freezen for adding this problem and creating all test cases.

    Subscribe to see which companies asked this question

     1 public class Solution {
     2     public int rob(int[] nums) {
     3         if(nums == null || nums.length <1) return 0;
     4         if(nums.length == 1) return nums[0];
     5         if(nums.length == 2) return Math.max(nums[0],nums[1]);//将其分为两种情况:1.包括nums[0] 2.包括nums[nums.length-1]
     6         return Math.max(getRob(nums,0,nums.length-2), getRob(nums,1,nums.length-1));
     7     }
     8     
     9     public int getRob(int[] nums,int s,int e){
    10         int len = e - s + 1;
    11         int[] res = new int[len];
    12         res[0] = nums[s];
    13         res[1] = Math.max(nums[s],nums[s+1]);
    14         for(int i = 2;i<len;i++){
    15             res[i] = Math.max(res[i-2]+nums[s+i], res[i-1]);
    16         }
    17         return res[len-1];
    18     }
    19 }
  • 相关阅读:
    [BZOJ3160]万径人踪灭
    [BZOJ5212][ZJOI2018]历史
    [BZOJ3563&3569]DZY Loves Chinese
    [HDU4336]Card Collector
    [HDU4652]Dice
    [POJ3683]Priest John's Busiest Day
    ISODateTimeFormat 转换2019-08-15T00:36:49.366456463Z 日期格式
    GoTTY-K8S-Docker 终端
    【php】PHP对redis操作详解
    【tool】VLC播放rtmp协议
  • 原文地址:https://www.cnblogs.com/guoguolan/p/5452887.html
Copyright © 2011-2022 走看看