[LeetCode]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 most 20000.
Each element nums[i] is an integer in the range [1, 10000].
这是一题对我很有启发的动态规划。
解题思路:
先将数组的各个value放到另一个数组set中,其下标是value(和桶排序第一步一样),set中元素的值是index对应的数之和。(例如,数组1,3,3,4对应的桶,set[1] = 1,set[3] = 6,set[4] =4.)
设我们选中i元素时得到的分数是take[i],不选该元素时得到的分数是skip[i],动态规划的思路是:
第i个元素对应的take[i]是:
第i-1个元素不被选中的分数(也就是skip[i-1])+第i个元素的值(此时第i个元素被选中) 和
第i-1个元素被选中的分数(也就是take[i-1])(此时第i个元素不被选中)
之中最大的那个值。
第i个元素对应的skip[i]是:
第i-1个元素被选中的分数(也就是take[i-1])
一直到最后得出的take就是我们要的值。
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
int take = 0, skip = 0;
std::vector<int> set = (1001,0);
if(nums.empty()) return 0;
for(int i = 0; i < nums.size(); i++){
set[nums[i]] += nums[i];//初始化
}
for(int i = 0; i < nums.size(); i++){
/*
skip+set[i]:前一个元素不选的分数+当前元素选中的分数
take:前一个元素选中的分数(+当前元素不选的分数,也即是0)
*/
int temp = max(skip + set[i], take);
skip = take;//当前元素不选的分数 = 前一个元素选中的分数 + 0
take = temp;//更新当前元素选中的分数
}
return take;
}
};
[LeetCode]Delete and Earn题解(动态规划)的更多相关文章
- [LeetCode] Delete and Earn 删除与赚取
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- Leetcode 10. 正则表达式匹配 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- leetcode:House Robber(动态规划dp1)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- C#版 - Leetcode 306. 累加数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- C#版(击败100.00%的提交) - Leetcode 372. 超级次方 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- LC 740. Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
随机推荐
- python学习笔记-控制流(if for while break continue)
if语句 if语句用以检查条件:如果条件为真(True),将运行一块语句(称作 if-block 或 if 块),否则将运行另一块语句(称作 else-block 或 else 块).其中else 从 ...
- 实时监测input输入变化 jQuery
$('#production_name').on('input propertychange',function(){ alert('输入一个字弹一回'); });
- [javascript]—jQuery解析本地 XML 文档
Create a jQuery object using an XML string and obtain the value of the title node. <!doctype html ...
- 使用python 模仿mybinlog 命令 二进制分析mysql binlog
出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. ...
- chainWebpack 和 htmlWebpackPlugin搭配使用
const HtmlWebpackPlugin = require('html-webpack-plugin'); ... chainWebpack: config => { config .p ...
- 剑指offer——面试题32:从上到下打印二叉树
void BFS(BinaryTreeNode* pRoot) { if(pRoot==nullptr) { cout<<"empty binary tree!"< ...
- Mono For Android中完美使用百度地图SDK(v2.1.2&v2.1.3)(转)
在Xamarin Mono For Android的开发中,如果要使用第三方的jar,就必须进行绑定.通过创建Java Bindings Library项目来自动生成C#到java的代码映射代码,最终 ...
- WCF系列教程之WCF中的会话
本文参考自http://www.cnblogs.com/wangweimutou/p/4516224.html,纯属读书笔记,加深记忆 一.WCF会话简介 1.在WCF应用程序中,回话将一组消息相互关 ...
- HelloStruts2
第一个struts2项目: 前言 假 如 你 的 人 生 有 理 想,那 么 就 一 定 要 去 追,不 管 你 现 在 的 理 想 在 别 人 看 来是 多 么 的 可 笑 , 你 也 不 用 在 ...
- 剑指offer(11-20)编程题
二进制中1的个数 数值的整数次方 调整数组顺序使奇数位于偶数前面 链表中倒数第k个结点 反转链表 合并两个排序的链表 树的子结构 二叉树的镜像 顺时针打印矩阵 包含min函数的栈 11.输入一个整数, ...