当前位置: 首页 > news >正文

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<int>(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<int> 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];
    }


相关文章:

  • 打破传统,实战至上的网管师认证
  • LeetCode -- Invert Binary Tree
  • 使用简单ORM开发框架进行快速开发
  • LeetCode -- Largest Number
  • LeetCode -- Length of last word
  • 编程是一种“组合的艺术”
  • LeetCode -- Majority Element II
  • 3G上网卡招标,华为成最大赢家
  • LeetCode -- Nim Game
  • 近期阅读关注(200905)
  • LeetCode -- Pascal's Triangle II
  • LeetCode -- Permutation Sequence
  • FreeBSD中替换系统调用监视系统文件打开记录
  • LeetCode -- Remove Element
  • 刚做的H1N1猪流感分布图Demo
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • 【知识碎片】第三方登录弹窗效果
  • Asm.js的简单介绍
  • C学习-枚举(九)
  • docker容器内的网络抓包
  • JavaScript异步流程控制的前世今生
  • Laravel 实践之路: 数据库迁移与数据填充
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • vue脚手架vue-cli
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • (C语言)字符分类函数
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (八)Flask之app.route装饰器函数的参数
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (附源码)计算机毕业设计高校学生选课系统
  • (力扣)1314.矩阵区域和
  • (十八)SpringBoot之发送QQ邮件
  • (转) RFS+AutoItLibrary测试web对话框
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (转)详解PHP处理密码的几种方式
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .NET 使用 XPath 来读写 XML 文件
  • .NET开发不可不知、不可不用的辅助类(一)
  • []FET-430SIM508 研究日志 11.3.31
  • [AHOI2009]中国象棋 DP,递推,组合数
  • [CTSC2014]企鹅QQ
  • [DNS网络] 网页无法打开、显示不全、加载卡顿缓慢 | 解决方案
  • [dts]Device Tree机制
  • [EFI]Lenovo ThinkPad X280电脑 Hackintosh 黑苹果引导文件
  • [HTML]HTML5实现可编辑表格
  • [IE9] 解决了傲游、搜狗浏览器在IE9下网页截图的问题
  • [IE编程] WebBrowser控件的多页面浏览(Tabbed Browsing)开发接口
  • [MySQL]SQL优化之索引的使用规则
  • [MySQL]视图索引以及连接查询案列