当前位置:网站首页>[dynamic planning medium] leetcode 198. looting 740. delete and get points
[dynamic planning medium] leetcode 198. looting 740. delete and get points
2022-07-27 03:04:00 【Wow, Kaka, negative, positive】
LeetCode 198. raid homes and plunder houses
https://leetcode.cn/problems/house-robber
You are a professional thief , Plan to steal houses along the street . There is a certain amount of cash in every room , The only restriction on your theft is that adjacent houses are equipped with interconnected anti-theft systems , If two adjacent houses are broken into by thieves on the same night , The system will automatically alarm .
Given an array of non negative integers representing the storage amount of each house , Count you Without triggering the alarm , The maximum amount that can be stolen overnight .
Input :[1,2,3,1]
Output :4
explain : Steal 1 House No ( amount of money = 1) , And then steal 3 House No ( amount of money = 3).
Maximum amount stolen = 1 + 3 = 4 .
Input :[2,7,9,3,1]
Output :12
explain : Steal 1 House No ( amount of money = 2), Steal 3 House No ( amount of money = 9), Then steal 5 House No ( amount of money = 1).
Maximum amount stolen = 2 + 9 + 1 = 12 .
Add : From 1 House No. 1 starts looting or no 2 It's OK to start looting House No .
1. Design status
dp[i] Express 0~i The maximum amount of money you can get after home robbery .
2. Write the equation of state transition
If robbery i-1 No. 1 house, then you can't rob any more i House No , Only one house can be robbed , So the state transfer equation is :dp[i] = max(dp[i-1], dp[i-2] + nums[i])
3. Set the initial state
dp[0] = nums[0]dp[1] = max(nums[0], nums[1])
4. Perform state transition
5. Return to the final solution
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];
// The initial state
dp[0] = nums[0];
dp[1] = max(nums[1], nums[0]);
for(int i = 2; i < numsSize; ++i) {
// State shift
dp[i] = max(dp[i-1], dp[i-2] + nums[i]);
}
// Return to the final solution
return dp[numsSize-1];
}
( Expand )LeetCode 740. Delete and get points
https://leetcode.cn/problems/delete-and-earn/
Give you an array of integers nums , You can do something with it .
In every operation , Choose any one nums[i] , Delete it and get nums[i] Number of points . after , You have to delete it all be equal to nums[i] - 1 and nums[i] + 1 The elements of .
At first you have 0 Points . Return the maximum number of points you can get through these operations .
Input :nums = [2,2,3,3,3,4]
Output :9
int max(int a, int b) {
return a > b ? a : b;
}
// Home robbing code
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]
// take nums Array mapping sum = [0,0,2,3,1], sum[i] Express nums in i The number of
// use val The array records the sum of the same elements val = [0,0,4,9,4] ==> It turns into the problem of robbing families
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];
}
// Directly call the code of home robbing
return rob(val, 10001);
}
边栏推荐
- Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
- Arduino UNO +74hc164 water lamp example
- iNFTnews | “流量+体验”白衬e数字时装节引领数字时装新变迁
- Cloud development sleeping alarm clock wechat applet source code
- day6
- 小玩一个并行多线程MCU—MC3172
- ArduinoUNO驱动RGB模块全彩效果示例
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- Plato Farm全新玩法,套利ePLATO稳获超高收益
- 人们为什么热衷于给事物排序
猜你喜欢

Plato Farm全新玩法,套利ePLATO稳获超高收益

Cloud development sleeping alarm clock wechat applet source code

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

一道数学题,让芯片巨头亏了5亿美金!

手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

数据库读写分离和分库分表

Web3.0世界知识体系分享-什么是Web3.0

Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
随机推荐
调用JShaman的Web API接口,实现JS代码加密。
对象创建的流程分析
[二分查找简单题] LeetCode 35. 搜索插入位置,69. x 的平方根,367. 有效的完全平方数,441. 排列硬币
银河证券基金低佣金开户靠谱吗,可靠安全吗
ansible系列之:不收集主机信息 gather_facts: False
Talk about connection pools and threads
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
{“errcode“:44001,“errmsg“:“empty media data, hint: [1655962096234893527769663], from ip: 222.72.xxx.
软件测试相关试题知识点
Cloud development sleeping alarm clock wechat applet source code
Play a parallel multithreaded mcu-mc3172
八皇后编程实现
CS224W fall 课程 ---- 1.1 why Graphs ?
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
Applet utils
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
Use of formdata
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
[动态规划简单题] LeetCode 53. 最大子数组和