当前位置:网站首页>[动态规划中等题] LeetCode 198. 打家劫舍 740. 删除并获得点数
[动态规划中等题] LeetCode 198. 打家劫舍 740. 删除并获得点数
2022-07-27 00:21:00 【哇咔咔负负得正】
LeetCode 198. 打家劫舍
https://leetcode.cn/problems/house-robber
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
输入:[1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
输入:[2,7,9,3,1]
输出:12
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
补充:从第 1 号房屋开始打劫或者第 2 号房屋开始打劫皆可。
1. 设计状态
dp[i] 表示 0~i 家打劫后能获得的最高钱数。
2. 写出状态转移方程
若打劫 i-1 号房屋那么就不能再打劫 i 号房屋了,只能隔一个房屋打劫,所以状态转移方程为:dp[i] = max(dp[i-1], dp[i-2] + nums[i])
3. 设定初始状态
dp[0] = nums[0]dp[1] = max(nums[0], nums[1])
4. 执行状态转移
5. 返回最终的解
dp[numsSize - 1]
Solution
int max(int a, int b) {
return a > b ? a : b;
}
int rob(int* nums, int numsSize){
if (numsSize == 1) {
return nums[0];
}
int dp[101];
// 初始状态
dp[0] = nums[0];
dp[1] = max(nums[1], nums[0]);
for(int i = 2; i < numsSize; ++i) {
// 状态转移
dp[i] = max(dp[i-1], dp[i-2] + nums[i]);
}
// 返回最终的解
return dp[numsSize-1];
}
(扩展)LeetCode 740. 删除并获得点数
https://leetcode.cn/problems/delete-and-earn/
给你一个整数数组 nums ,你可以对它进行一些操作。
每次操作中,选择任意一个 nums[i] ,删除它并获得 nums[i] 的点数。之后,你必须删除 所有 等于 nums[i] - 1 和 nums[i] + 1 的元素。
开始你拥有 0 个点数。返回你能通过这些操作获得的最大点数。
输入:nums = [2,2,3,3,3,4]
输出:9
int max(int a, int b) {
return a > b ? a : b;
}
// 打家劫舍代码
int rob(int* nums, int numsSize){
if (numsSize == 1) {
return nums[0];
}
int dp[numsSize];
dp[0] = nums[0];
dp[1] = max(nums[1], nums[0]);
for(int i = 2; i < numsSize; ++i) {
dp[i] = max(dp[i-1], dp[i-2] + nums[i]);
}
return dp[numsSize-1];
}
int deleteAndEarn(int* nums, int numsSize){
// nums = [2,2,3,3,3,4]
// 将 nums 数组进行映射 sum = [0,0,2,3,1], sum[i] 表示 nums 中 i 的数量
// 用 val 数组记录相同元素之和 val = [0,0,4,9,4] ==> 转化为了打家劫舍问题
int sum[10001];
int val[10001];
memset(sum, 0, sizeof(sum));
for (int i = 0; i < numsSize; ++i) {
sum[nums[i]]++;
}
for (int i = 0; i < 10001; ++i) {
val[i] = i * sum[i];
}
// 直接调用打家劫舍的代码
return rob(val, 10001);
}
边栏推荐
- 素因子分解--C(gcc)--PTA
- B-树的应用以及添加和删除操作
- Turn: YuMinHong: what hinders your growth is yourself
- Play a parallel multithreaded mcu-mc3172
- 百度云人脸识别
- 测试人需要的数据库知识:MySQL常用语法
- Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
- [Ryu] common problems and solutions in installing Ryu
- Ten thousand words long text, take you to understand the kubernetes network model
- iNFTnews | “流量+体验”白衬e数字时装节引领数字时装新变迁
猜你喜欢

iNFTnews | GGAC联合中国航天ASES 独家出品《中国2065典藏版》

Rust web (I) -- self built TCP server

Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round

小玩一个并行多线程MCU—MC3172

函数栈帧详解

基于GoLang实现API短信网关

Time module: acquisition and mutual transformation of timestamp, structured time and formatted time

Functions of libraries and Archives

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩

Cs224w fall course - --- 1.1 why graphs?
随机推荐
系统安全测试要怎么做,详细来说说
【RYU】安装RYU常见问题及解决办法
聊聊连接池和线程
关于url编解码应该选用的函数
JMeter interface test, quickly complete a single interface request
Rust web (I) -- self built TCP server
How small programs help the new model of smart home ecology
BP plug-in temporary code record
Web3.0世界知识体系分享-什么是Web3.0
Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
Talk about connection pools and threads
Applet utils
听说你们面试都跪在了接口测试上?
Goatgui invites you to attend a machine learning seminar
快速排序(Quick sort)
单例模式(双检锁)
C语言程序的编译(预处理)下
Jmeter接口测试, 快速完成一个单接口请求
哈希表与一致性哈希的原理理解以及应用
万字长文,带你搞懂 Kubernetes 网络模型