• 题目描述

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. C# 读写xml、excel、word、ppt、access

    C# 读写xml.excel.word.access 这里只是起个头,不做深入展开,方便以后用到参考 读写xml,主要使用.net  的xml下的document using System;using ...

  2. vue学习笔记之基础篇

    本文主要记录学习vue的一些基础内容及常用知识点的记录. 1.搭建脚手架 vue init webpack vue-demo 初始化一个使用webpack打包的vue项目 npm install 安装 ...

  3. 首页的css

    html,body{ margin:; padding:; background-color: lavenderblush; } a{ color:darkgray; } li{ list-style ...

  4. ES6新增的math,Number方法

    ES6新增的math,Number方法,下面总结了一些我觉得有用的 Nunber.isInteger()判断是否为整数,需要注意的是1,和1.0都会被认为是整数 console.log(Number. ...

  5. Java设计模式—门面模式(带案例分析)

    1.门面模式的定义: 门面模式(Facade Pattern)也叫做外观模式,是一种比较常用的封装模式,其定义如下:       要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行.门面模式 ...

  6. Flexviewer使用Google地图作为底图

    Flexviewer使用Google地图作为底图: 在使用google地图作底图前提是你需要在Flex中实现加载google地图的代码(网上一大堆,随便找), 在只加载google地图的情况下,成功显 ...

  7. 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色

    简介 在简单恢复模式下,日志文件的作用仅仅是保证了SQL Server事务的ACID属性.并不承担具体的恢复数据的角色.正如”简单”这个词的字面意思一样,数据的备份和恢复仅仅是依赖于手动备份和恢复.在 ...

  8. Tomcat中部署web应用的三种方式

    Tomcat中部署web应用的三种方式(静态部署)       第一种,针对war或解压后的war,最为常用的是直接操作webapp目录,将完整的war包或者web应用直接放到webapp目录下.使用 ...

  9. mysql导入导出csv

    LOAD DATA local INFILE '/tmp/stb.csv' INTO TABLE stb FIELDS TERMINATED BY ',' enclosed by '"' l ...

  10. Python中深浅拷贝 垃圾回收与 super继承(六)

    1 python拷贝 深拷贝,浅拷贝 与引用三者的区别 import copy a = [1, 2, 3, 4, ['a', 'b']] #原始对象 b = a #赋值,传对象的引用 c = copy ...