LeetCode 213
2016-05-11 17:43
288 查看
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.
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 == 0 ) { return 0; } if( numsSize == 1 ) { return nums[0]; } int max = 0; int prev1 = 0; int prev2 = 0; int i, temp; temp = 0; prev1 = 0; prev2 = 0; for( i=0; i<=numsSize-2; i++ ) { temp = prev1; prev1 = (prev2+nums[i])>prev1 ? (prev2+nums[i]) : prev1; prev2 = temp; } max = prev1; temp = 0; prev1 = 0; prev2 = 0; for( i=1; i<=numsSize-1; 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[] = { 2,1,1,4,7,3,0 }; int numsSize = 7; int ret = rob( nums, numsSize ); printf("%d\n", ret); return 0; }
相关文章推荐
- Linux命令行下常用的快捷键
- NGUI的UISprite动态染色的一种方法
- 在Linux上安装postgresql
- SHA1算法升级SHA256更新计划
- java 获取程序执行时间
- thinkphp I()方法参数含义,新手要常用
- 性能更好的js动画实现方式——requestAnimationFrame
- MVC 4 异步编程简化了
- 数据访问......简单练习
- python计算文件的md5值
- HDU4161 Iterated Difference(模拟 + 递推)
- c++第五次作业
- LeetCode 198
- Hibernate 一对一映射
- hdu 1556 线段树
- selenium java等待alert对话框弹出,并关闭对话框
- 自动匹配HTTP请求中对应实体参数名的数据(性能不是最优)
- 网游中的网络编程3:在UDP上建立虚拟连接
- 资料
- 两个数组实现堆栈