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的更多相关文章

  1. [LeetCode] 213. House Robber II 打家劫舍之二

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  2. [LeetCode] 213. House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  3. Java实现 LeetCode 213 打家劫舍 II(二)

    213. 打家劫舍 II 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗 ...

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

  5. 【LeetCode 213】House Robber II

    This is an extension of House Robber. After robbing those houses on that street, the thief has found ...

  6. LeetCode 213. House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  7. leetcode 213. 打家劫舍 II JAVA

    题目: 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻 ...

  8. Leetcode 213.大家劫舍II

    打家劫舍II 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两 ...

  9. [leetcode] #213 House Robber II Medium (medium)

    原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...

随机推荐

  1. MySQL分支Percona, cmake编译安装

    Percona是在MySQL基础上改进的,在功能和性能上有着很显著的提升.该版本提升了在高负载情况下的InnoDB的性能,为DBA提供一些非常有用的性能诊断工具:另外有更多的参数和命令来控制服务器行为 ...

  2. homework-08-作业2

    1. 了解Lambda的用法 计算“Hello World!”中 a.字母‘e’的个数 b. 字母‘l’的个数 代码: void calcEL() { char s[100] = "Hell ...

  3. Spring4整合Hibernate4详细示例

    1. Spring整合Hibernate,主要是解决什么问题? a.让Spring提供的IOC容器来管理Hibernate的SessionFactory b.让Hibernate使用Spring提供的 ...

  4. hud 1166 敌兵布阵

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  5. jdk自带发布webservice服务

    1.创建要发布的类 package com.test.webserive; import javax.jws.WebService; //targetNamespace定义命名空间 @WebServi ...

  6. AutoCAD.NET二次开发:创建自定义菜单的两种方法比较

    目前我已经掌握的创建CAD菜单方法有两种: COM方式: http://www.cnblogs.com/bomb12138/p/3607929.html CUI方式: http://www.cnblo ...

  7. Android应用开发学习之相对布局

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边.右边.上边或下 ...

  8. Listener 监听器

    Listener的定义与作用 监听器Listener就是在application,session,request三个对象创建.销毁或者往其中添加修改删除属性时自动执行代码的功能组件. Listener ...

  9. 更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found

    在Xcode中当你在更新了你得证书而再重新编译你的程序,真机调试一直会出现 Code Sign error: Provisioning profile ‘XXXX’ can't be found是不是 ...

  10. C#中反射的使用(How to use reflect in CSharp)(2)

    在上一篇里,我们叨逼了好多如何获取到程序集里的对象,但是对象有了,还不知道怎么调,OK,下面开始干这个对象: 首先,我们对上一篇的对象做了一些修改,以适应多种情况: using System; usi ...