博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
House Robber
阅读量:4696 次
发布时间:2019-06-09

本文共 1254 字,大约阅读时间需要 4 分钟。

House Robber

 
Total Accepted: 212 
Total Submissions: 780
Question  Solution 

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

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.

Credits:

Special thanks to  for adding this problem and creating all test cases. Also thanks to  for adding additional test cases.

题目的意思是:从数组中任选N个数相加,要求取得的和最大。前提是,随意两个数不能相邻。

记S[I]表示前i个元素所可以取得的最大和,则S[I+1]=max(S[i],S[i-1]+num[i+1]),

则代码例如以下:

public class Solution {    public int rob(int[] num) {        if(num.length==0) return 0;        int[] s=new int[num.length];        s[0]=num[0];        for(int i=1;i
1){ s[i]=Math.max(s[i-1],s[i-2]+num[i]); }else{ s[i]=Math.max(s[i-1],num[i]); } } return s[num.length-1]; }}

转载于:https://www.cnblogs.com/jhcelue/p/6773694.html

你可能感兴趣的文章
面向对象和面向过程的区别及优劣对比详解
查看>>
const与指针
查看>>
thsi指针的一些用法及作用
查看>>
c++友元
查看>>
c++运算符重载
查看>>
一元运算符重载
查看>>
Windows 远程栈溢出挖掘
查看>>
(网页)the server responded with a status of 403 (Forbidden)
查看>>
葡萄城报表介绍:Java 报表
查看>>
android 通知消息一
查看>>
UNET学习笔记2 - 高级API(HLAPI)
查看>>
腾讯编程马拉松2012第一题
查看>>
Day18
查看>>
Web Service数据源
查看>>
php.ini详解(转)
查看>>
[转]基于Python的接口测试框架
查看>>
"ORA-00942: 表或视图不存在 "的原因和解决方法[转]
查看>>
PeekMessage、GetMessage的区别
查看>>
磁盘使用率达到100%
查看>>
linux跳过root密码登陆
查看>>