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-05)
0. 摘要 之前我们玩了2次黄金数游戏,我也幸运的得到了一本<代码大全>,嘿嘿.这次的作业是一个Client/Server程序,自动化完成多轮重复游戏. 我完成了Client部分,使用C# ...
- 现代程序设计 homework-08
现代程序设计 homework-08 第八次作业. 理解C++变量的作用域和生命周期 作用域就是一个变量可以被引用的范围,如:全局作用域.文件作用域.局部作用域:而生命周期就是这个变量可以被引用的时间 ...
- 糟糕的双重检查加锁(DCL)
在Java并发编程时,同步都会存在着巨大的性能开销,因此,人们使用了很多的技巧来降低同步的影响,这其中有一些技巧很好,但是也有一些技巧存在一些缺陷,下面要结束的双重检查加锁(DCL)就是有缺陷的一类. ...
- 在Hibernate中使用HibernateTemplate来进行包含sql语句的查询
/** * 使用sql语句进行查询操作 * @param sql * @return */ public List queryWithSql(final Stri ...
- SSH三大框架整合使用的配置文件 注解实现
1 Struts.xml 使用拦截器 <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE str ...
- 教你50招提升ASP.NET性能(十七):不要认为问题只会从业务层产生
(28)Don’t assume that problems can only arise from business logic 招数28: 不要认为问题只会从业务层产生 When beginnin ...
- C语言用static限制函数以及全局变量的作用域
今天才发现这个东西! C语言中没有public private之类的东西. 如果一个函数或者一个全局变量只想在一个.c文件中使用,可以在前面加上static! 以前我还傻傻的每个.c文件中的函数都加一 ...
- spring-mvc整合freemarker并在ftl模版中显示服务端校验的错误信息,JSR303或者JSR349
写法有多种,应该可以任意组合,最重要的是要引入spring.ftl 1.Bean里面的就不再多写了,来个简单就可以了 @NotEmpty(message="用户密码码不可为空") ...
- delphi 去掉TreeView水平滚动条
使用API函数:声明 FUNCTION ulong ShowScrollBar(ulong hwnd,ulong wBar,ulong bShow) LIBRARY "user32. ...
- search result
https://github.com/search?l=java&p=86&q=Floating+window&type=Code&utf8=%E2%9C%93http ...