LeetCode 740. Delete and Earn
原题链接在这里:https://leetcode.com/problems/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].
题解:
Sort the numbers into bucket.
If you take the current bucket, you can't take next to it.
include[i] denotes max points earned by taking bucket i, include[i] = exclude[i-1] + i*buckets[i].
exclude[i] denotes max points earned by skipping bucket i, exclude[i] = Math.max(include[i-1], exclude[i-1]).
Time Complexity: O(nums.length + range). range = 10000.
Space: O(range).
AC Java:
class Solution {
public int deleteAndEarn(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int n = 10001;
int [] buckets = new int[n];
for(int num : nums){
buckets[num]++;
}
int in = 0;
int ex = 0;
for(int i = 0; i<n; i++){
int inclusive = ex + i*buckets[i];
int exclusive = Math.max(in, ex);
in = inclusive;
ex = exclusive;
}
return Math.max(in, ex);
}
}
类似House Robber.
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
题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...
- 【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 ...
- [LeetCode] Delete and Earn 删除与赚取
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- [Swift]LeetCode740. 删除与获得点数 | Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- [LeetCode] Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- [LeetCode] Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...
随机推荐
- LeetCode 5198. 丑数 III(Java)容斥原理和二分查找
题目链接:5198. 丑数 III 请你帮忙设计一个程序,用来找出第 n 个丑数. 丑数是可以被 a 或 b 或 c 整除的 正整数. 示例 1: 输入:n = 3, a = 2, b = 3, c ...
- go标准库I/O模型:epoll+多协程
本文为linux环境下的总结,其他操作系统本质差别不大.本地文件I/O和网络I/O逻辑类似. epoll+多线程的模型 epoll+多线程模型和epoll 单进程区别.优点 对比于redis这 ...
- 用LabVIEW做声源定位系统
前一阵子,研发部举办了为期三天的第一届Innovation Day,让大家用3天时间去完成工作之外的一些创意.有人做微信小程序,有人继续研究一些工作中用到的Tool,有人把一直想解决而没时间解决的老b ...
- HTML文件直接在浏览器打开和本地服务器localhost打开有什么区别?
最直接的区别,很容易注意到,一个是file协议,另一个是http协议. file协议更多的是将该请求视为一个本地资源访问请求,和你使用资源管理器打开是一样的,是纯粹的请求本地文件. http请求方式则 ...
- MOOC 数据库笔记(四):关系代数
关系代数 关系代数概述 特点 基于集合,提供了一系列的关系代数操作:并.差.笛卡尔积(广义积).选择.投影和更名等基本操作 以及交.连接和关系除等扩展操作,是一种集合思维的操作语言. 关系代数操作以一 ...
- httpd服务的配置及应用
一.httpd服务的配置文件 httpd服务的主配置文件通常为httpd根目录下的conf/httpd.conf文件,通过yum安装的httpd服务的主配置路径通常如下: httpd-2.2:/etc ...
- C# vb .net实现博物馆哑色框架特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的博物馆哑色框架效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授 ...
- 使用@Async注解创建多线程,自定义线程池
说明 使用@Async注解创建多线程非常的方便,还可以通过配置,实现线程池.比直接使用线程池简单太多.而且在使用上跟普通方法没什么区别,加上个@Async注解即可实现异步调用. 用法 AsyncTas ...
- P1108 低价购买 (DP)
题目 P1108 低价购买 解析 这题做的我身心俱惫,差点自闭. 当我WA了N发后,终于明白了这句话的意思 当二种方案"看起来一样"时(就是说它们构成的价格队列一样的时候),这2种 ...
- ffmpeg 把视频转换为图片
ffmpeg -i "Tail of Hope.mp4" -r 1 -q:v 2 -f image2 pic-%03d.jpeg