当前位置:网站首页>[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);
}
边栏推荐
- JS utils fragmented
- 银河证券基金低佣金开户靠谱吗,可靠安全吗
- 系统安全测试要怎么做,详细来说说
- Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"
- Database read-write separation and database and table segmentation
- Interview shock 68: why does TCP need three handshakes?
- [栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
- OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
- 朴素贝叶斯——文档分类
- Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
猜你喜欢

Invalid target distribution: solution for 17

制作ppt时间轴

Database knowledge required by testers: MySQL common syntax

Play a parallel multithreaded mcu-mc3172

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

If you want to thoroughly optimize the performance, you must first understand the underlying logic~

人们为什么热衷于给事物排序

Non global function of lua function

批量复制宝贝上传提示乱码,如何解决?

基于GoLang实现API短信网关
随机推荐
C language program compilation (preprocessing)
Rust web (I) -- self built TCP server
Use of formdata
CAS部署使用以及登录成功跳转地址
CS224W fall 1.2 Applications of Graph ML
Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
Kubernetes dashboard deployment application and access
Go to export excel form
[SQL简单题] LeetCode 627. 变更性别
White box test case design (my grandfather can understand it)
Concept of data asset management
Knowledge points of test questions related to software testing
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
全网最全的软件测试基础知识整理(新手入门必学)
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
五、MFC视图窗口和文档
[二分查找简单题] LeetCode 35. 搜索插入位置,69. x 的平方根,367. 有效的完全平方数,441. 排列硬币
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
杀毒软件 clamav 的安装和使用