当前位置:网站首页>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);
}边栏推荐
- Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
- Invalid revision: 3.18.1-g262b901-dirty
- C # character similarity comparison general class
- QT get random color value and set label background color code
- Thinkphp6.0 middleware with limited access frequency think throttle
- js获取对象中嵌套的属性值
- 配置交叉编译工具链和环境变量
- 1480. Dynamic sum of one-dimensional array
- JS execution mechanism
- How to implement lazy loading in El select (with search function)
猜你喜欢

The end of the Internet is rural revitalization

Upper computer software development - log information is stored in the database based on log4net

ANSYS command

Configure cross compilation tool chain and environment variables

How to expand all collapse panels

A little understanding of GSLB (global server load balance) technology

How to configure static IP for Kali virtual machine

win10清除快速访问-不留下痕迹

input显示当前选择的图片

Halcon image calibration enables subsequent image processing to become the same as the template image
随机推荐
Impact relay jc-7/11/dc110v
如何避免 JVM 内存泄漏?
How to implement lazy loading in El select (with search function)
体验碎周报第 102 期(2022.7.4)
Arc135 C (the proof is not very clear)
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
剑指 Offer II 038. 每日温度
Kubernets first meeting
Install pytoch geometric
js arguments参数使用和详解
el-select如何实现懒加载(带搜索功能)
VB. Net simple processing pictures, black and white (class library - 7)
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Qt发布多语言国际化翻译
[microservice] Nacos cluster building and loading file configuration
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
卸载Google Drive 硬盘-必须退出程序才能卸载
How much computing power does transformer have
每周小结(*63):关于正能量
Design and implementation of redis 7.0 multi part AOF