当前位置:网站首页>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);
}边栏推荐
- Impact relay jc-7/11/dc110v
- left_and_right_net正常版本
- JSON web token -- comparison between JWT and traditional session login authentication
- BUU-Crypto-Cipher
- Upper computer software development - log information is stored in the database based on log4net
- One click filtering to select Baidu online disk files
- 测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
- Webrtc quickly set up video call and video conference
- Kubernets first meeting
- px em rem的区别
猜你喜欢

How to get the parent node of all nodes in El tree

冲击继电器JC-7/11/DC110V

ANSYS command

Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor

如何展开Collapse 的所有折叠面板

Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式

VB. Net GIF (making and disassembling - optimizing code, class library - 5)

检漏继电器JY82-2P

接地继电器DD-1/60

HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)
随机推荐
每周小结(*63):关于正能量
Invalid revision: 3.18.1-g262b901-dirty
Basic concept of bus
【无标题】
Wechat applet +php realizes authorized login
Thinkphp6.0 middleware with limited access frequency think throttle
My NVIDIA developer journey - optimizing graphics card performance
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
Practical gadget instructions
High performance parallel programming and optimization | lesson 02 homework at home
卸载Google Drive 硬盘-必须退出程序才能卸载
Leakage detection relay jy82-2p
VB. Net simple processing pictures, black and white (class library - 7)
Google Chrome browser will support the function of selecting text translation
Uninstall Google drive hard drive - you must exit the program to uninstall
transformer坑了多少算力
Design and implementation of tcp/ip series overview
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Tutle clock improved version
buuctf-pwn write-ups (8)