LeetCode 213
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.
/*************************************************************************
> File Name: LeetCode213.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Wed 11 May 2016 17:11:02 PM CST
************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> /*
跟198的区别在于现在是一个环形,首尾不能同时get
所以分成两种情况:
1:从头取到尾-1
2:从头+1取到尾
rob过程相同,然后比较两种情况大小
*/
int rob( int* nums, int numsSize )
{
if( numsSize == )
{
return ;
}
if( numsSize == )
{
return nums[];
} int max = ;
int prev1 = ;
int prev2 = ; int i, temp; temp = ;
prev1 = ;
prev2 = ;
for( i=; i<=numsSize-; i++ )
{
temp = prev1;
prev1 = (prev2+nums[i])>prev1 ? (prev2+nums[i]) : prev1;
prev2 = temp;
}
max = prev1; temp = ;
prev1 = ;
prev2 = ;
for( i=; i<=numsSize-; i++ )
{
temp = prev1;
prev1 = (prev2+nums[i])>prev1 ? (prev2+nums[i]) : prev1;
prev2 = temp;
}
max = max>prev1 ? max : prev1; return max;
} int main()
{
int nums[] = { ,,,,,, };
int numsSize = ; int ret = rob( nums, numsSize );
printf("%d\n", ret);
return ;
}
LeetCode 213的更多相关文章
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- Java实现 LeetCode 213 打家劫舍 II(二)
213. 打家劫舍 II 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗 ...
- Java for LeetCode 213 House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 【LeetCode 213】House Robber II
This is an extension of House Robber. After robbing those houses on that street, the thief has found ...
- LeetCode 213. House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- leetcode 213. 打家劫舍 II JAVA
题目: 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻 ...
- Leetcode 213.大家劫舍II
打家劫舍II 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两 ...
- [leetcode] #213 House Robber II Medium (medium)
原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...
随机推荐
- 现代程序设计——homework-09
Lambda表达式 // homework-09.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream ...
- 【转】iOS 硬件授权检测:定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等
iOS系统版本的不断升级的前提,伴随着用户使用设备的安全性提升,iOS系统对于App需要使用的硬件限制也越来越严格,App处理稍有不妥,轻则造成功能不可用用户还不知道,重则会造成App Crash. ...
- HDU 5742 It's All In The Mind (贪心)
It's All In The Mind 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5742 Description Professor Zhan ...
- JSF 2 outputText example
In JSF 2.0 web application, "h:outputText" tag is the most common used tag to display plai ...
- Spring Filter components in auto scanning
In this Spring auto component scanning tutorial, you learn about how to make Spring auto scan your c ...
- datasnap的前世今生
随着XE6,XE7,以及半年以后即将发布的XE8,DATASNAP将顺应跨平台的需要, 有可能的情况是这样的:XE8,DATASNAP写的中间件将可以运行在LINUX服务器上面. 大家都知道COM是W ...
- ajax 传参 乱码问题
http://blog.csdn.net/yiyuhanmeng/article/details/7548505 开发一直用firfox网页,调试什么的都很方便.所以遇到了浏览器之间的兼容问题.url ...
- List、ArrayList、Vector及map、HashTable、HashMap分别的区别
一.List与ArrayList的区别 List->AbstractList->ArrayList (1) List是一个接口,ArrayList是一个实现了List接口 ...
- Tomcat7中配置Oracle 11g数据库DBCP连接池
将 ojdbc6.jar tomcat-jdbc-7.0.37.jar 拷贝到工程的WEB-INF\lib 下面 一.在Tomcat的配置文件Tomca ...
- 谈谈C#基元类型
首先看一下.NET 中的基元类型,如下表: C# Type | .NET Framework Type -------------| ---------------------- bool | Sys ...