leetcode笔记(六)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
nums
is at most20000
. - Each element
nums[i]
is an integer in the range[1, 10000]
.
- 解题思路
看题目,可以意识到是动态规划类型的题目,但不知道怎么写迭代式子,就是相不清楚状态。所以,一开始心虚的按照自己的贪婪算法实现了下,结果就是代码极多,但结果不能对~
无奈,开始查阅资料,找到了一篇比较靠谱的博客。借鉴他的思路,自己努力写了下动态规划的实现。思路的关键在于:
- 取出一个数,其收益为 数的频数 × 数的值。按照规则,取出一个,必然取出该值的所有数。
- 两个状态,取出当前数的最大收益(maxFetch),不取当前数的最大收益(maxNoFetch)。
- 初始状态:
- maxFetch = 0, maxNoFetch = 0;
- 当前状态与上一状态的关系
- 不取当前数,则为上一状态的最大值(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的更多相关文章
- LC 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
原题链接在这里: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 ...
- 740. Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- [LeetCode]Delete and Earn题解(动态规划)
Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...
- # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)
目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- Java 的版本历史与特性
Java SE 8[2014-03-14发行] Lambda表达式 Pipelines和Streams Date和Time API Default方法 Type注解 Nashhorn JavaScri ...
- Python pip windows安装
参考页面: [1] : https://pip.pypa.io/en/latest/installing.html [2] : http://stackoverflow.com/questions/4 ...
- github使用手册
1.git init 2.git add README.md (增加文件夹/文件:git add dir/files) 3.git commit -m "注释内容” 4.git push - ...
- vue 音乐播放器报错
使用Vue报错[Vue warn]: Error in nextTick: "TypeError: fn.bind is not a function"页面进不去. 检查:看看da ...
- solidity语言11
函数修饰符 pragma solidity ^0.4.11; contract owned { address owner; // 构造函数 function owned() public { own ...
- supervisor运行virtualenv环境下的nagios-api
supervisord-example.conf [unix_http_server] file=/tmp/supervisor.sock ; path to your socket file [su ...
- mongodb 3.4 学习 (二)命令
# 使用或切换数据库 use <database name> # 显示所有数据库 show dbs # 显示所有collection show collections # 显示所有user ...
- SQL Server 的 主键 解决方案 NEWID() , 自增ID
在 SQL Server 表的主键有自增Id ,和 GUID. 1. 自增Id 优点:索引空间小,索引连续.在大量数据插入的时候性能有特别大的优势. 缺点:可移植性差,在数据迁移的时候. 2. G ...
- MyISAM引擎表出现“Error 'Incorrect key file for table”
mysql主从复制中的从库突然出现了警报,sql_thread停止了,show slave status\G;查看 mysql> show slave status\G ; . row **** ...
- React学习笔记 - Hello World
React Learn Note 1 React学习笔记(一) 标签(空格分隔): React JavaScript 前.Hello World 1. 创建单页面应用 使用Create React A ...