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 内存模型(主内存,工作内存)

    JVM将内存组织为主内存和工作内存两个部分. 主内存是所有的线程所共享的,主要包括本地方法区和堆. 每个线程都有一个工作内存不是共享的,工作内存中主要包括两个部分: 1:一个是属于该线程私有的栈; 2 ...

  2. RabbitMQ的持久化(六)

    RabbitMQ的持久化主要体现在三个方面,即交换机持久化,队列持久化及消息持久化 注意,因公司使用php-amqplib来实现RabbitMQ,故之后举例说明的代码均使用的php-amqplib,而 ...

  3. cbv装饰器 中间件 跨站请求伪造

    给cbv下面的函数加装饰器 写一个验证用户登录的程序 前端页面 # 写一个装饰器验证session def login_auth(func): def inner(request,*args,**kw ...

  4. 万能模拟器eve-ng介绍

    作为一名网络工程师,通过模拟器来搭建实验环境是必不可少的,主流的网络模拟器有如下几款:Packet Tracer是思科公司出的一款模拟器,只能模拟思科自己的设备,由于此款软件是纯软件模拟,所以思科设备 ...

  5. (转)android(SignalA)接收.net(SignalR)推送过来的消息

    从网络上搜索到的Demo,自己进行了稍微的改动 Signala类库从https://github.com/erizet/SignalA获得,不过相关引用有错误,需要手动修正. 下载相关源码 packa ...

  6. python的内置函数(一)

    1.数学计算函数 abs(x) 求绝对值1.参数可以是整型,也可以是复数2.若参数是复数,则返回复数的模 complex([real[, imag]]) 创建一个复数 divmod(a, b) 分别取 ...

  7. Unrecognized SSL message, plaintext connection? 将https 换为http 即可

    请求链接:https://59********* 升级后的项目地址有https换为了http  ,出现这个错误,改为http请求即可

  8. triggerHandler(type, [data])

    triggerHandler(type, [data]) 概述 这个特别的方法将会触发指定的事件类型上所有绑定的处理函数.但不会执行浏览器默认动作,也不会产生事件冒泡. 大理石平台价格 这个方法的行为 ...

  9. 微信小程序开发入门教程(三)---小程序云开发支付功能

    支付(shoukuan)功能真的很重要!由于我还没有商户号,以下代码未实际验证 1.服务端 进入云开发,新建云函数pay(应该也可以在开发者工具编写后上传) 编写后端代码index.js这里用到第三方 ...

  10. linux系统编程--信号

    信号的概念 man 7 siganl  查看man手册 信号在我们的生活中随处可见, 如:古代战争中摔杯为号:现代战争中的信号弹:体育比赛中使用的信号枪......他们都有共性:1. 简单 2. 不能 ...