leetcode 198 House Robber
今天看了一个华为西安研究院的一个女生代码大神的总结很有感悟,下面这句话送给大家:
只有好的程序员才能写出人类可以理解的代码
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.
the objective function is basically:
dp(i) = max(dp[i-2] + num[i], dp[i-1]),
this means the current max is the max of the position i-2 plus the current num[i], or the max of the previous one i-1 (cannot including num[i] with i-1 position, otherwise it will trigger the alarm)
我的解决方案:
class Solution {
public:
int rob(vector<int>& nums)
{
if(nums.empty())return 0;
int length = nums.size();
vector<int> dp(length,0);
dp[0] = nums[0];
dp[1] = max(nums[0],nums[1]);
for(int i =2; i< length; ++i)
{
dp[i] = max(dp[i-2]+nums[i],dp[i-1]);
}
return dp[length-1];
}
};
c语言解决方案:
#define max(a, b) ((a)>(b)?(a):(b))
int rob(int num[], int n) {
int a = 0;
int b = 0;
for (int i=0; i<n; i++)
{
if (i%2==0)
{
a = max(a+num[i], b);
}
else
{
b = max(a, b+num[i]);
}
}
return max(a, b);
}
python解决方案:
class Solution:
# @param num, a list of integer
# @return an integer
def rob(self, num):
# DP O(n) time, O(1) space
# ik: max include house k
# ek: max exclude house k, (Note: ek is also the maximum for house 1,...,k-1)
# i[k+1]: num[k] + ek #can't include house k
# e[k+1]: max(ik, ek) # can either include house k or exclude house k
i, e = 0, 0
for n in num: #from k-1 to k
i, e = n+e, max(i,e)
return max(i,e)
- 基于TensorFlow实现自编码器(附源码)
- Selenium2+python自动化53-unittest批量执行(discover)
- HTML/CSS/JavaScript学习笔记【持续更新】
- Selenium2+python自动化54-unittest生成测试报告(HTMLTestRunner)
- Selenium2+python自动化55-unittest之装饰器(@classmethod)
- 每天一个Linux命令(4)——mkdir
- 每天一个Linux命令(3)——pwd
- 11-移动端开发教程-zepto.js入门教程
- 【OpenCV学习笔记之一】图像加载,修改及保存
- 【干货】一种直观的方法认识梯度下降
- 漫谈Java IO之普通IO流与BIO服务器
- 浅谈强化学习的方法及学习路线
- 【亲测有效】Win10家庭版Microsoft Edge页面出现乱码的两种解决方案及gpedit.msc命令无法使用的解决策略
- Fiddler抓包7-post请求(json)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android自定义View验证码输入框
- PHP生成随机字符串实例代码(字母+数字)
- Laravel框架中缓存的使用方法分析
- android实现搜索功能并将搜索结果保存到SQLite中(实例代码)
- Android实现全局右滑返回
- Android实现打地鼠小游戏
- android实现手机传感器调用
- Android实现接近传感器
- PHP 模拟登陆功能实例详解
- PHP判断一个变量是否为整数、正整数的方法示例
- android实现打地鼠游戏
- Yii框架连表查询操作示例
- Android studio listview实现列表数据显示 数据循环显示效果
- 关于Curl在Swoole协程中的解决方案详析
- Flutter随机迷宫生成和解迷宫小游戏功能的源码