leetcode笔记(六)740. Delete and Earn
2018-06-29 06:14:48来源:博客园 阅读 ()
- 题目描述
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 = 0; i < size; i++) { int curr = nums[i]; // remember frequences for curr if(freqs.find(curr) == freqs.end()) { freqs[curr] = 1; } else { freqs[curr] += 1; } } int maxFetch = 0, maxNoFetch = 0; int prevMaxFetch = 0, prevMaxNoFetch = 0; 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 = 0; } // transferring else { prevMaxFetch = maxFetch; prevMaxNoFetch = maxNoFetch; // do not get the number maxNoFetch = max(prevMaxFetch, prevMaxNoFetch); // get this number if(currChoice->first == prevChoice -> first + 1 || currChoice->first == prevChoice -> first - 1) { // 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); } };
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- leetcode 反转链表 2020-06-06
- OpenCV开发笔记(五十九):红胖子8分钟带你深入了解分水岭 2020-05-24
- 算法笔记刷题6 ( PAT 1003我要通过 ) 2020-05-08
- C++基础 学习笔记六:复合类型之数组 2020-04-25
- C++基础 学习笔记五:重载之运算符重载 2020-04-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash