当前位置:网站首页>740. Delete and get points
740. Delete and get points
2022-07-04 06:01:00 【mrbone9】
Address :
Power button https://leetcode-cn.com/problems/delete-and-earn/
subject :
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 .
Example 1:
Input :nums = [3,4,2] Output :6 explain : Delete 4 get 4 Points , therefore 3 Also deleted . after , Delete 2 get 2 Points . All in all 6 Points . |
Example 2:
Input :nums = [2,2,3,3,3,4] Output :9 explain : Delete 3 get 3 Points , And then we're going to delete two 2 and 4 . after , Delete again 3 get 3 Points , Delete again 3 get 3 Points . All in all 9 Points . |
Tips :
1 <= nums.length <= 2 * 10^4 1 <= nums[i] <= 10^4 |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/delete-and-earn
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Select one i, Delete i-1 and i -2 , Just choose one , The next two cannot be selected , Typical 198. raid homes and plunder houses
But there are still some small details to deal with , That is, there are duplicate elements in the array , Therefore, it cannot be applied directly
So if we count the number of elements , And put the sum value in the new array
This new array can be applied
This part of the work is entrusted to Counting sorting of sorting algorithm complete
Method 1 、 Count array + raid homes and plunder houses
#define max(a,b) ( (a) > (b) ? (a) : (b) )
int rob(int* nums, int numsSize){
int i;
int dp[10001]={0};
dp[0] = nums[0];
for(i=1; i<numsSize; i++)
{
if(i == 1)
dp[i] = max(nums[0], nums[1]);
else
dp[i]= max(dp[i-1], dp[i-2]+nums[i]);
}
return dp[numsSize - 1];
}
int deleteAndEarn(int* nums, int numsSize){
int i;
int count[10001] = {0};
int val[10001] = {0};
for(i=0; i<numsSize; i++)
{
int idx = nums[i];
count[idx]++;
}
for(i=0; i<10001; i++)
{
val[i] = i * count[i];
}
return rob(val, 10001);
}
边栏推荐
- 卸载Google Drive 硬盘-必须退出程序才能卸载
- Uninstall Google drive hard drive - you must exit the program to uninstall
- Sword finger offer II 038 Daily temperature
- [openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
- My NVIDIA developer journey - optimizing graphics card performance
- 如何获取el-tree中所有节点的父节点
- Nexus 6p downgraded from 8.0 to 6.0+root
- Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
- Design and implementation of redis 7.0 multi part AOF
- VB. Net GIF (making and disassembling - optimizing code, class library - 5)
猜你喜欢
How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
transformer坑了多少算力
接地继电器DD-1/60
buuctf-pwn write-ups (8)
Grounding relay dd-1/60
SQL injection - injection based on MSSQL (SQL Server)
BUU-Real-[PHP]XXE
Detectron: train your own data set -- convert your own data format to coco format
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
C # character similarity comparison general class
随机推荐
VB. Net simple processing pictures, black and white (class library - 7)
Actual cases and optimization solutions of cloud native architecture
How to determine whether an array contains an element
【无标题】
buuctf-pwn write-ups (8)
Excel comparator
BUU-Crypto-Cipher
How to implement lazy loading in El select (with search function)
复合非线性反馈控制(二)
QT qtablewidget table column top requirements ideas and codes
How to solve the component conflicts caused by scrollbars in GridView
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
The end of the Internet is rural revitalization
How much computing power does transformer have
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
2022.7.3-----leetcode.556
LC weekly 300
(4) Canal multi instance use
JSON Web Token----JWT和傳統session登錄認證對比
FRP intranet penetration, reverse proxy