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
numsis 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 ...
随机推荐
- POJ 3667 线段树区间合并
http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 用线段树,首先要定义好线段树的节点信息,一般看到一个问题,很难很 ...
- Java web service 异常
1.org/apache/commons/discovery/tools/DiscoverSingleton Exception in thread "main" java.lan ...
- Android开发ListView嵌套ImageView实现单选按钮
做Android开发两年的时间,技术稍稍有一些提升,刚好把自己实现的功能写出来,记录一下,如果能帮助到同行的其他人,我也算是做了件好事,哈哈!!废话不多说,先上个图. 先上一段代码: if (last ...
- caffe-windows之网络描述文件和参数配置文件注释(mnist例程)
caffe-windows之网络描述文件和参数配置文件注释(mnist例程) lenet_solver.prototxt:在训练和测试时涉及到一些参数配置,训练超参数文件 <-----lenet ...
- CSS深入理解学习笔记之padding
1.padding与容器尺寸之间的关系 对于block水平元素:①padding值暴走,一定会影响尺寸:②width非auto,padding影响尺寸:③width为auto或box-sizing为b ...
- 避免console错误,console兼容
背景:写js代码时写了很多console.log进行日志打印,最后上生产时不想删除日志输出, 但是ie在不打开控制台时,日志输出会导致后续js不执行,所以需要适时屏蔽js日志输出 (IE等不支持con ...
- Sharepoint 2013企业内容管理学习笔记(一) 半自动化内容管理
大家好,今天我来与大家分享一个关于sharepoint2013文档管理方面的一个知识,我相信也许早就有人了解并熟知这项技术了,呵呵,众所周知,sharepoint 有一个很亮的功能,什么?没错,就是文 ...
- join......on 后面的and 和where的区别
a.where 是在两个表join完成后,再附上where条件. b. and 则是在表连接前过滤A表或B表里面哪些记录符合连接条件,同时会兼顾是left join还是right join.即 假如是 ...
- JUnit_BeforeClass不报异常的 bug 处理
1.try{} cathce(Exception e){}将觉得会出问题的地方括起来测试. 2.main方法调用出问题的方法.
- 使用版本 1.0.0 的 Azure ARM SDK for Java 创建虚拟机时报错
问题描述 我们可以通过使用 Azure ARM SDK 来管理 Azure 上的资源,因此我们也可以通过 SDK 来创建 ARM 类型的虚拟机,当我们使用 1.0.0 版本的 Azure SDK fo ...