LeetCode House 罗布ber II

日期:2019-11-15编辑作者:最快开奖计算机

LeetCode House Robber II

LeetCode -- 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.

与以前的率先个本子雷同,只是这一次的数组首尾的皇家赛马会被作为是相邻的,即无法同临时候抢第1匹和最后1匹。

思路:
核心仍旧接受DP来解,只是须求基于第1种解法思索三种特别景况就可以(抢第1匹丢弃最终后生可畏匹和放任第1匹抢最终1匹卡塔尔:

  1. 对第[0,n-1]匹马推行DP , 拿到max1
  2. 对第[1,n]匹马试行DP,拿到max2

最终回来max1与max2的最大值。

瞩目,由于是环,因而小于4匹寅时,只必要再次来到数组最大值就可以。

得以完结代码:

public int Rob(int[] nums) 
    {
        if(nums == null || nums.Length == 0)
     {
      return 0;
     }

     if(nums.Length < 4){
      return nums.Max();
     }

     var list = new List(nums);
     var first = list[0];
     list.RemoveAt(0);
     var max1 = Max(list);

     list.Insert(0 , first);
     list.RemoveAt(list.Count-1);
     var max2 = Max(list);

     return Math.Max(max1 , max2);
    }




    private int Max(IList nums)
    {
     var len = nums.Count;
     var dp = new int[len + 1];
     dp[0] = 0;
     dp[1] = Math.Max(nums[0], 0);

     for(var i = 2;i < len + 1; i++){
      dp[i] = Math.Max(dp[i-1], dp[i-2] + nums[i-1]);
     }

     return dp[len];
    }

 

-- House 罗布ber II 标题陈说: Note: This is an extension of House 罗布ber. After robbing those houses on that street, the thief has found himself a new place for his t...

LeetCode House Robber II

题目

图片 1

思路

思路来源于Discuss。
一是在罗布ber这题中的O(n)解法;
二是在罗布ber那题中,大家只必要各自考虑富含了nums[0]和nums[n-1]的景况就可以。
注意这里的隐含并非说罗布ber一定要偷num[0]或nums[n-1],只是说考虑进去的乐趣。

代码

#define max(a, b) ((a)>(b)?(a):(b))

int rob(int* nums, int numsSize) {
    if (numsSize <= 0) return 0;
    if (numsSize == 1) return nums[0];
    int evenMax, ooddMax, include0, i;
    for (i = evenMax = ooddMax = 0; i < numsSize - 1; i++)  // 包含nums[0]
        if (i % 2) ooddMax = max(ooddMax + nums[i], evenMax);
        else evenMax = max(evenMax + nums[i], ooddMax);
    include0 = max(evenMax, ooddMax);
    for (i = 1, evenMax = ooddMax = 0; i < numsSize; i++)  // 包含nums[n-1]
        if (i % 2) ooddMax = max(ooddMax + nums[i], evenMax);
        else evenMax = max(evenMax + nums[i], ooddMax);
    return max(include0, max(evenMax, ooddMax));
}

House 罗布ber II LeetCode House 罗布ber II 标题 思路 思路来源于Discuss。 一是在罗布ber那题中的O(n)解法; 二是在罗布ber那题中,大家只需求各高等教育自学考试...

本文由今晚最快开奖现场直播发布于最快开奖计算机,转载请注明出处:LeetCode House 罗布ber II

关键词:

aes加密算法的一个问题

aes加密算法的二个标题,aes加密算法难题 百度“delphi aes”出来的结果,千篇生机勃勃律,都是互相转发,但是从未人...

详细>>

Java开发之路

[Java开拓之路](15)注解 1. 简介   注明(也被称得上元数据卡塔尔,为大家在代码中增加消息提供了生龙活虎种情势...

详细>>

Nginx集群解决均衡负载及生产环境热部署,ID的持

平常的,假若三个顾客展开多个浏览器,服务器就能分配多个sessionid,通过session.getId()函数能够回来session的id; 像这种...

详细>>

实现指纹识别登录,在Ubuntu上安装fprint实现指纹

ThinkPad T420i 上 Ubuntu12.04 完毕指纹识别登入 实验室配的新计算机有指纹识别,才装好Ubuntu没二日,正好折腾了生气勃勃...

详细>>