• 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 }
  • 相关阅读:
    HelloCSS-Border
    开发你的第一个NCS(Zephyr)应用程序
    NanoPi R4S (RK3399) openssl speed 硬件加解密性能测试结果
    Flink-状态
    Flink-时间语义和Watermark
    Flink-Window
    Flink-运行时架构
    Flink-流处理wordcount
    Flink-批处理wordcount
    设计模式七大原则-合成复用原则
  • 原文地址:https://www.cnblogs.com/guoguolan/p/5452887.html
Copyright © 2020-2023  润新知