当前位置:网站首页>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);
}边栏推荐
- AWT介绍
- 我的NVIDIA开发者之旅——优化显卡性能
- Component、Container容器常用API详解:Frame、Panel、ScrollPane
- Install pytoch geometric
- Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
- Compound nonlinear feedback control (2)
- js获取对象中嵌套的属性值
- Descriptive analysis of data distribution characteristics (data exploration)
- Arc135 a (time complexity analysis)
- ansys命令
猜你喜欢

LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout

C语言练习题(递归)
![[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx](/img/a9/72791cbcc6c9da45e89450ab2820c1.jpg)
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx

实用的小工具指令

BUU-Reverse-easyre

How much computing power does transformer have

Gridview出现滚动条,组件冲突,如何解决

注释与注解

Halcon图片标定,使得后续图片处理过后变成与模板图片一样

BUU-Crypto-Cipher
随机推荐
Design and implementation of redis 7.0 multi part AOF
C # character similarity comparison general class
Use of hutool Pinyin tool
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
js获取对象中嵌套的属性值
Experience weekly report no. 102 (July 4, 2022)
MySQL的information_schema数据库
Lightroom import picture gray / Black rectangular multi display
gslb(global server load balance)技术的一点理解
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
lightroom 导入图片灰色/黑色矩形 多显示器
2022.7.3-----leetcode.556
FRP intranet penetration, reverse proxy
[excel] PivotChart
How to get the parent node of all nodes in El tree
如何获取el-tree中所有节点的父节点
How to expand all collapse panels
How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
el-select如何实现懒加载(带搜索功能)
如何避免 JVM 内存泄漏?