当前位置:网站首页>740. Delete and get points
740. Delete and get points
2022-07-21 17:02:00 【baixiaofei567】
The comment said it was a house robbery 4, It's very figurative .
The meaning of this question is to delete a number to obtain points , Then force the deletion i+1 and i-1 And you can't get points , We need to get the maximum number of points .
Let's go through nums, Record the number of occurrences of each number , And record the largest number .
It mainly depends on whether to take the current figure , If you take the front, you can't take , If you don't take the front, you can take ,0 I just didn't take it ,1 It's just taking
State definition :dp[i][1] Is to delete the current number ,dp[i][0] Just don't delete the current number
State shift : If you take the front, you can't take ( The previous ones are forcibly deleted ), If you don't take the front, you can take it instead , Take it, then =dp[i-1][0] + ima[i], Use the current number Number of occurrences
Finally back to dp[maxn][0] and [1] The bigger one
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
int n = nums.size();
int maxn = 0;
unordered_map<int,int> ma;
for(int i = 0; i < n; ++i){
ma[nums[i]] += 1;
maxn = max(maxn,nums[i]);
}
vector<vector<int>> dp(maxn+1,vector<int>(2,0));
// It mainly depends on whether to take the current figure , If you take the front, you can't take , If you don't take the front, you can take
//0 I just didn't take it ,1 It's just taking
for(int i = 1; i <= maxn; ++i){
dp[i][1] = dp[i-1][0] + i * ma[i];
dp[i][0] = max(dp[i-1][1],dp[i-1][0]);
}
return max(dp[maxn][0],dp[maxn][1]);
}
};
边栏推荐
猜你喜欢
【保研】-- 保研夏令营中开放性问题回答
全国职业院校技能大赛网络安全B模块 “web安全之综合渗透测试”
GAMES101图形学P10笔记(geometry1)
Ython + selenium web automation 2022 updated tutorial automated testing software test crawler - Notes blog collation
【FLink】Flink 任务 如何优雅的停止
938. 二叉搜索树的范围和
前 3 名突然变了,揭秘 7 月编程语言最新排行榜
[Baoyan] - answer to open questions in Baoyan summer camp
Network security module B of national vocational college skills competition "comprehensive penetration test of web security"
740. 删除并获得点数
随机推荐
第131篇 ERC20 锁仓合约
(pc+wap) Zhimeng template air purification website
FlinkCDC
First day of scala study (Hello World)
让我们用ArcGIS制作一张好看的中国月度气温图
《强化学习周刊》第54期:i-Sim2Real、GriddlyJS & 逆强化学习(IRL)最新应用
cannot convert parameter 1 from 'class A' to 'class A'
Swift 中监听属性值变化 (观察者模式)
LeetCode 1260 二维网格迁移[数组] HERODING的LeetCode之路
1723. 完成所有工作的最短时间
B2B企业的5大数字化转型战略
Go语言 错误处理
Reinforcement learning weekly 54: i-sim2real, griddlyjs & the latest application of inverse reinforcement learning (IRL)
scala WordCount案例
2.5 亿、巴南区感知体系数据采集、后台分析、雪亮工程网络及运维服务项目:中国电信中标
自签名SAN证书
Pedestrian recognition Reid
第132篇 mapping 中的删除操作
我来告诉你,一个草根程序员如何逆袭,成功进入BAT!
Do you need to go to the business department of the securities company to adjust the commission? Is it safe to open an account on your mobile phone