leetcode weekly contest 61 ( 740. Delete and Earn )
来源:互联网 发布:淘宝盗图申诉包过 编辑:程序博客网 时间:2024/05/03 09:09
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: 6Explanation: 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: 9Explanation: 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 most 20000.Each element nums[i] is an integer in the range [1, 10000].
题目
说我们可以从一堆数中挑选一个数 , 然后删除比这个数大1和小1的数,然后得到挑选这个数的分
问最后最多可以拿多少分
分析
首先如果我们有很多数这里数有重复那么我们一旦拿了其中一个数,就一定会拿完
因为当我们拿了一个数后,它已经没有这个数+1 和这个数-1 了,如果我们不拿完这个数,那么一定会剩下来
那么这个问题就简化了
接着是一个决策问题
很容易看到每一次都会作出选择是否拿这个数
并且最大化利益 opt = max
按顺序排序数
定义dp[i]代表拿这个数所能获得的最大利益
状态转移 如果前一项等于我们这个值-1
那么我们拿前一项的前一项的dp[i-2]+这项获得的利益 和前一项dp[i-1]比较
代码
class Solution {public: int deleteAndEarn(vector<int>& nums) { if( nums.size() == 0 ) return 0 ; map< int , int > hash ; for( int i=0 ; i<nums.size() ; i++){ hash[nums[i]]++; } vector< int > vals; for( auto it = hash.begin() ; it!= hash.end() ; it++){ vals.push_back( it->first ); } int ans = 0 ; vector< int > dp ; dp.resize( vals.size() , 0 ) ; dp[0] = vals[0]*hash[vals[0]]; for( int i=1 ; i<vals.size() ; i++){ if( vals[i-1] + 1 == vals[i] ){ if( i-2 >=0 ){ dp[i] = max( dp[i-2] + vals[i]*hash[vals[i]] , dp[i-1] ) ; } else{ dp[i] = max( vals[i]*hash[vals[i]] , dp[i-1] ) ; } } else{ dp[i] = dp[i-1]+vals[i]*hash[vals[i]]; } } return dp.back() ; }};
时间复杂度O(n∗logn) 因为我们排序了可以通过hash降到O(n)
空间复杂度 O(n)
阅读全文
0 0
- leetcode weekly contest 61 ( 740. Delete and Earn )
- Leetcode:740. Delete and Earn
- LeetCode | 740. Delete and Earn
- [leetcode] 740. Delete and Earn
- [LeetCode]740. Delete and Earn
- Leetcode 740. Delete and Earn
- Leetcode | Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- LWC 61:740. Delete and Earn
- Git的Patch功能
- 17.12.05,web学习第十九天,还有一年,努力吧青年事务mysql
- Kylin系列-大数据分析界的“神兽”Apache Kylin有多牛?
- 深度学习与强化学习
- 汽车IC测试技术(这将帮助您更轻松地入睡)---电子技术资料
- leetcode weekly contest 61 ( 740. Delete and Earn )
- 有关 Azure IaaS VM 磁盘以及托管和非托管高级磁盘的常见问题解答
- Servlet如何处理多个请求访问?
- 最好学习网址Java、php、redis.......
- cocos2dx打包APK爬坑
- js 大厦之JavaScript事件
- 创新工场汪华:OMO时代,谁是下一个领跑者?
- 一文读懂AlphaGo背后的强化学习
- 人工智能到底威胁人类还是造福人类?