• 题目描述

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:

  1. The length of nums is at most 20000.
  2. Each element nums[i] is an integer in the range [1, 10000].
  • 解题思路

看题目,可以意识到是动态规划类型的题目,但不知道怎么写迭代式子,就是相不清楚状态。所以,一开始心虚的按照自己的贪婪算法实现了下,结果就是代码极多,但结果不能对~

无奈,开始查阅资料,找到了一篇比较靠谱的博客。借鉴他的思路,自己努力写了下动态规划的实现。思路的关键在于:

  1. 取出一个数,其收益为 数的频数 × 数的值。按照规则,取出一个,必然取出该值的所有数。
  2. 两个状态,取出当前数的最大收益(maxFetch),不取当前数的最大收益(maxNoFetch)。
  3. 初始状态:
    •  maxFetch = 0, maxNoFetch = 0;
  4. 当前状态与上一状态的关系
    • 不取当前数,则为上一状态的最大值(max(prevMaxFetch, prevMaxNoFetch))。
    • 取出当前数,若数和上一状态的数关联(+/- 1),则为prevMaxNoFetch + 取出数的收益。否则,为max(prevMaxFetch, prevMaxNoFetch) + 取出数的收益。
  • 示例代码
class Solution {
public: int deleteAndEarn(vector<int>& nums) {
map<int, int> freqs;
int size = nums.size();
for(int i = ; i < size; i++)
{
int curr = nums[i];
// remember frequences for curr
if(freqs.find(curr) == freqs.end())
{
freqs[curr] = ;
}
else
{
freqs[curr] += ;
} } int maxFetch = , maxNoFetch = ;
int prevMaxFetch = , prevMaxNoFetch = ;
map<int, int>::iterator prevChoice;
map<int, int>::iterator currChoice;
// calculate maximum according to previous status
for(currChoice = freqs.begin(); currChoice != freqs.end(); ++currChoice)
{
// initiate
if(currChoice == freqs.begin())
{
// get this number
maxFetch = currChoice->first * currChoice->second;
// do not get this number
maxNoFetch = ;
}
// transferring
else
{
prevMaxFetch = maxFetch;
prevMaxNoFetch = maxNoFetch;
// do not get the number
maxNoFetch = max(prevMaxFetch, prevMaxNoFetch);
// get this number
if(currChoice->first == prevChoice -> first + || currChoice->first == prevChoice -> first - ) {
// related -> must not fetch previous node
maxFetch = prevMaxNoFetch + currChoice->first * currChoice->second;
}
else
{ // non related
maxFetch = maxNoFetch + currChoice->first * currChoice->second;
}
} prevChoice = currChoice;
} return max(maxFetch, maxNoFetch);
}
};

leetcode笔记(六)740. Delete and Earn的更多相关文章

  1. LC 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

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

  3. 【leetcode】740. Delete and Earn

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

  4. 740. Delete and Earn

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

  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. # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)

    目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. My first python application

    ''' Authon:WSE_Gordon This application is use for the costomer to login the application. The Costome ...

  2. NIOSocket Server Client

    最近在看Netty框架,顺便写了一下NIO SocketChannel服务端和客户端 Server.java import java.io.IOException; import java.net.I ...

  3. Python进阶篇四:Python文件和流

    摘要: Python对于文件和流的操作与其他编程语言基本差不多,甚至语句上比其他语言更为简洁.文件和流函数针对的对象除了这两者之外还有,类文件(file-like),即python中只支持读却不支持写 ...

  4. YCRefreshView-自定义支持上拉加载更多,下拉刷新。。。

    自定义支持上拉加载更多,下拉刷新,支持自由切换状态[加载中,加载成功,加载失败,没网络等状态]的控件,拓展功能[支持长按拖拽,侧滑删除]可以选择性添加 .具体使用方法,可以直接参考demo. 轻量级侧 ...

  5. 重复启动某一款应用,并传递intent参数

    Intent intent = getPackageManager().getLaunchIntentForPackage(packageName); intent.setFlags(Intent.F ...

  6. 有关在新版mac上 git 环境变量的配置问题

    前段时间买的新版 mpb ,各种环境什么都没有配置,想着在网上边搜边摸索着将各种开发工具逐步配置齐全,各种问题不断出现,不知道是不是新版的原因不兼容. 其中 git 的配置尤为奇怪.在git官网上直接 ...

  7. log4j2单独的配置与使用&log4j2+slf4j的结合的配置与使用

    转载自:https://github.com/iamyong 一.log4j2单独的配置与使用 所用jar文件 log4j-api-2.8.2.jar log4j-core-2.8.2.jar 配置文 ...

  8. selenium(2.4.0)中不能导出web drive 代码

    最近在学习selenium.要做web自动化测试.咱只会C# .就想了,这个测试用例要是能用C# 控制,保存起来,就可以以后自动运行了,不用每次点击运行了. 看了看它的文档,就开始试验了,录脚本折腾了 ...

  9. spring与mybatis的整合

    整合的思路 SqlSessionFactory对象放到spring容器中作为单例存在. 传统dao的开发方式中,从spring容器中获得sqlsession对象. Mapper代理形式中,从sprin ...

  10. github 专案介绍 – Python 范例:透过互动式的 Jupyter 和数学解释流行的机器学习演算法

    对于机器学习有兴趣,不少人应该会先从 Andrew Ng ( 吴恩达 ) 的机器学习课程开始,但是吴恩达的课程是使用 octave 这个工具当作练习.这个 github 项目包含使用 Python 实 ...