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 ...
随机推荐
- Java基础教程(26)--反射
一.类 对于每一种类型的对象,Java虚拟机都会实例化一个java.lang.Class类的不可变实例.该实例提供了获取对象的运行时属性的方法,包括它的成员和类型信息.Class类还提供了创建新实 ...
- Django框架之DRF APIView Serializer
一.APIView 我们在使用DjangoRestfulFramework的时候会将每个视图类继承APIView,取代原生Django的View类 APIView的流程分析: rest_framewo ...
- Jenkins+harbor+gitlab+k8s 部署maven项目
一.概述 maven项目部署流程图如下: 环境介绍 操作系统 ip 角色 版本 ubuntu-16.04.4-server-amd64 192.168.10.122 Jenkins+harbor Je ...
- java之hibernate之 cascade和inverse
1.Cascade是级联动作,在many_to_one中如果使用cascade可以级联操作关联对象,如下代码可以级联保存Category对象. 在Book的映射文件设置 <many-to-one ...
- How to do SSH Tunneling (Port Forwarding)
How to do SSH Tunneling (Port Forwarding) In this post we will see how ssh works?, what is SSH tunne ...
- Ubuntu中安装(升级)GraphicsMagick
1 前言 采用官方下载安装包然后强制安装升级Ubuntu中有的老版本gmagick-1.3.28,升级到gmagick-1.3.31 仅用来记录使用. 2.准备工作 1.下载安装包 https://s ...
- Kafka 生产者、消费者与分区的关系
背景 最近和海康整数据对接, 需要将海康产生的结构化数据拿过来做二次识别. 基本的流程: 海康大数据 --> kafka server --> 平台 Kafka 的 topic 正常过车 ...
- Excel 2010同时打开2个或多个独立窗口
亲测有效 参考下面的网址 https://jingyan.baidu.com/article/86fae346acca7d3c49121ad4.html 1. 在win+r 输入框里面输入“rege ...
- A Deep Dive Into Draggable and DragTarget in Flutter
https://medium.com/flutter-community/a-deep-dive-into-draggable-and-dragtarget-in-flutter-487919f6f1 ...
- pandas-12 数学计算操作df.sum()、df.min()、df.max()、df.decribe()
pandas-12 数学计算操作df.sum().df.min().df.max().df.decribe() 常用的数学计算无非就是加减,最大值最小值,方差等等,pandas已经内置了很多方法来解决 ...