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 nums is at most 20000.
  • Each element nums[i] is an integer in the range [1, 10000].
Runtime: 4852 ms, faster than 0.72% of C++ online submissions for Delete and Earn.

自以为做了一个区间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的更多相关文章

  1. 740. Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  2. leetcode笔记(六)740. Delete and Earn

    题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...

  3. LeetCode 740. Delete and Earn

    原题链接在这里:https://leetcode.com/problems/delete-and-earn/ 题目: Given an array nums of integers, you can ...

  4. 【leetcode】740. Delete and Earn

    题目如下: Given an array nums of integers, you can perform operations on the array. In each operation, y ...

  5. [LeetCode]Delete and Earn题解(动态规划)

    Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...

  6. [LeetCode] Delete and Earn 删除与赚取

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  7. [Swift]LeetCode740. 删除与获得点数 | Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. JAVA语言程序设计课后习题----第七单元解析(仅供参考)

    1 本题水题,就是想让你理解继承的含义 public class Animaal { public double weight; public void eat(){ } } public class ...

  2. Hadoop_21_MapReduce程序实现Join功能

    1.序列化与Writable接口 1.1.hadoop的序列化格式 序列化和反序列化就是结构化对象和字节流之间的转换,主要用在内部进程的通讯和持久化存储方面 hadoop在节点间的内部通讯使用的是RP ...

  3. python再学习笔记

    python各种半桶水QAQ,一些特性经常跟其他语言搞混,官方入门文档重读温习...... 最好用4个空格的缩进空值是Python里一个特殊的值,用None表示变量就是在程序中用来指向这些数据对象的, ...

  4. 基于递归的BFS(Level-order)

    上篇中学习了二叉树的DFS深度优先搜索算法,这次学习另外一种二叉树的搜索算法:BFS,下面看一下它的概念: 有些抽象是不?下面看下整个的遍历过程的动画演示就晓得是咋回事啦: 了解其概念之后,下面看下如 ...

  5. Beego框架POST请求接收JSON数据

    原文: https://blog.csdn.net/Aaron_80726/article/details/83870563 ------------------------------------- ...

  6. MyBatis-11-一对多处理

    11.一对多处理 比如:一个老师拥有多个学生! 对于老师而言,就是一对多的关系! 环境搭建 环境搭建,和刚才一样 实体类 @Data public class Teacher { private in ...

  7. python_tkinter组件摆放方式

    1.最小界面组成 # 导入tkinter模块 import tkinter # 创建主窗口对象 root = tkinter.Tk() # 设置窗口大小(最小值:像素) root.minsize(30 ...

  8. GLSLPROGRAM METALPROGRAM unity

    https://docs.unity3d.com/Manual/SL-GLSLShaderPrograms.html unity里面可以直接写原生的shader 用相应的宏包起来 CGPROGRAM ...

  9. 16-SQLServer强制走索引

    一.注意点 1.使用with(index(索引名称))来使SQL强制走索引. 二.示例截图 1.创建非聚集索引 2.不使用with,不走索引的截图 3.使用with,强制走索引的截图

  10. django nginx uwsgi 502 Gateway

    前提:腾讯云服务器有个内网ip和外网ip 首先检查使用的端口是否正常可用 1.检查端口是否开放,在腾讯云控制台安全组查看 2.检查防火墙端口是否开放 systemctl start firewalld ...