LC 740. Delete and Earn
Given an array nums of integers, you can perform operations on the array.
In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
You start with 0 points. Return the maximum number of points you can earn by applying such operations.
Example 1:
Input: nums = [3, 4, 2]
Output: 6
Explanation:
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.
Example 2:
Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation:
Delete 3 to earn 3 points, deleting both 2's and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned.
Note:
- The length of
numsis at most20000. - Each element
nums[i]is an integer in the range[1, 10000].
自以为做了一个区间DP,结果并不是这样做。
//
// Created by yuxi on 2019/1/22.
// #include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std; class Solution {
public:
unordered_map<int,int> mp;
unordered_map<int,int> memo;
int deleteAndEarn(vector<int>& nums) {
if(nums.empty()) return ;
for(int x : nums) mp[x]++;
vector<int> keys;
for(auto it = mp.begin(); it != mp.end(); it++) keys.push_back(it->first);
sort(keys.begin(),keys.end());
vector<vector<int>> dp(keys.size(), vector<int>(keys.size(),));
for(int k=; k<keys.size(); k++) {
for(int i=; i<keys.size(); i++) {
int j = i - k;
if(j < ) continue;
if(i == j) {
dp[j][i] = keys[i] * mp[keys[i]];
continue;
}
for(int l = j; l < i; l++) {
if(keys[l] == keys[l+]-) {
if(l+ == i) dp[j][i] = max(dp[j][i], max(dp[j][l],dp[i][i]));
else dp[j][i] = max(dp[j][i], dp[j][l]+dp[l+][i]);
}else {
dp[j][i] = max(dp[j][i], dp[j][l]+dp[l+][i]);
}
}
}
}
return dp[][keys.size()-];
}
};
Runtime: 8 ms, faster than 87.68% of C++ online submissions for Delete and Earn.
//
// Created by yuxi on 2019/1/22.
// #include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std; class Solution {
public:
unordered_map<int,int> mp;
unordered_map<int,int> memo;
int deleteAndEarn(vector<int>& nums) {
if(nums.empty()) return ;
int maxval = ;
for(int x : nums) {
maxval = max(maxval,x);
mp[x]++;
}
vector<int> a(maxval+, );
for(auto it=mp.begin(); it != mp.end(); it++) {
a[it->first] = it->second;
}
int prev = , cur = ;
for(int i=; i<a.size(); i++) {
cur = max(cur, i > ? a[i] * i + a[i-] : a[i] * i);
a[i] = cur;
}
return cur;
}
};
LC 740. Delete and Earn的更多相关文章
- 740. Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- leetcode笔记(六)740. Delete and Earn
题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...
- LeetCode 740. Delete and Earn
原题链接在这里:https://leetcode.com/problems/delete-and-earn/ 题目: Given an array nums of integers, you can ...
- 【leetcode】740. Delete and Earn
题目如下: Given an array nums of integers, you can perform operations on the array. In each operation, y ...
- [LeetCode]Delete and Earn题解(动态规划)
Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...
- [LeetCode] Delete and Earn 删除与赚取
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- [Swift]LeetCode740. 删除与获得点数 | Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- LC 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- LC 955. Delete Columns to Make Sorted II
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose an ...
随机推荐
- 配置了ssh免密登录,仍然需要输入密码
1 权限问题 原因 权限问题 chmod -R 700 ~/.ssh
- Oracle笔记(十四) 用户管理
SQL语句分为三类:DML.DDL.DCL,之前已经讲解完了DML和DDL,现在就差DCL操作的,DCL主要表示的是数据库的控制语句,控制的就是操作权限,而在DCL之中,主要有两个语法:GRANT.R ...
- shell脚本编程进阶及RAID和LVM应用1
bash脚本编程 脚本文件格式: 第一行,顶格写: #!/bin/bash 注释行:#开头 代码注释:写清楚注释 规范写脚本:适度缩进,添加空白行 编程语言:有编程语法格式,库,算法和数据结构 编程思 ...
- <meta http-equiv="X-UA-Compatible" content="IE=edge">详解
X-UA-Compatible是针对IE8新加的一个设置,对于IE8之外的浏览器是不识别的. 这个区别与content="IE=7"在无论页面是否包含<!DOCTYPE> ...
- c#客户端自动更新模块
一.概述 将需要更新的文件上传到服务器端,然后客户端从服务器下载更新文件并覆盖本地文件. 二.功能模块 1.将更新文件放入指定文件夹,检测更新,生成更新配置文件,并上传到服务器 2.获取服务器的更新配 ...
- InheritableThreadLocal——父线程传递本地变量到子线程的解决方式及分析
转自https://blog.csdn.net/hewenbo111/article/details/80487252 上一个博客提到ThreadLocal变量的基本使用方式,可以看出ThreadLo ...
- Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)
链接: https://codeforces.com/contest/1230/problem/E 题意: Kamil likes streaming the competitive programm ...
- 【git】git中使用https和ssh协议的区别以及它们的用法
git可以使用四种主要的协议来传输资料: 本地协议(Local),HTTP 协议,SSH(Secure Shell)协议及 git 协议.其中,本地协议由于目前大都是进行远程开发和共享代码所以一般不常 ...
- shell拷贝文件到另一台机器
#!/bin/bash data=$(date "+%Y-%m-%d %H:%M:%S") ip='192.168.10.14' password='fan' gitBak='/v ...
- pycharm mysql数据源配置、SQL方言配置
会发现有提示,看着不爽,但不影响运行程序, 这里提示没有配置数据源,现在配置MYSQL数据源 然后看到右边Database选项卡,点击 然后可能会出现网络防火墙提示,选择全部允许,之后可能会在pych ...