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
nums
is 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 ...
随机推荐
- c++11多线程记录6:条件变量(condition variables)
https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...
- 不一样的go语言-玩转语法之一
这段时间为俗事所累,疲以应付,落下了不少想法,错过了更新的日子.这个专题开始之际,已经准备了不下十几个主题,而在写作的过程中,又有新想法与主题涌现出来.未来预计想写写的内容主要包括: 玩转语法系列 ...
- (五)pdf的构成之文件体(catalog对象)
引自:https://blog.csdn.net/steve_cui/article/details/82735039 目录(catalog): 文档目录包含对定义文档内容的其他对象的引用.它还包含声 ...
- XML和Json的特点
Xml特点: 1.有且只有一个根节点: 2.数据传输的载体 3.所有的标签都需要自定义 4.是纯文本文件 Json(JavaScript Object Notation)特点: json分为两种格式: ...
- Python进阶----UDP协议使用socket通信,socketserver模块实现并发
Python进阶----UDP协议使用socket通信,socketserver模块实现并发 一丶基于UDP协议的socket 实现UDP协议传输数据 代码如下:
- Falsk框架 Session 与 Flask-Session
目录 Cookie 与 Session 简单了解 Falsk 中 Session 的保管机制 相关的配置 使用 Flask-Session 三方组件 基础练习题 Cookie 与 Session 简单 ...
- java Document生成和解析xml
转自:https://blog.csdn.net/p812438109/article/details/81807440 Document场景:需要知道文档所有结构 需要把文档一些元素排序 文档中的信 ...
- Android存储及getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()区别
存储介绍 Android系统分为内部存储和外部存储,内部存储是手机系统自带的存储,一般空间都比较小,外部存储一般是SD卡的存储,空间一般都比较大,但不一定可用或者剩余空间可能不足.一般我们存储内容都会 ...
- 教你如何配置linux用户实现禁止ssh登陆机器但可用sftp登录!
构想和目标最近有个这样的诉求:基于对线上服务器的保密和安全,不希望开发人员直接登录线上服务器,因为登录服务器的权限太多难以管控,如直接修改代码.系统配置,并且也直接连上mysql.因此希望能限制开发人 ...
- Android笔记(七十六) 点菜DEMO
一个朋友让看一下他的代码,一个点菜的功能,他和我一样,初学者,代码比我的都混乱,也是醉了,干脆想着自己写个demo给他看,原本想着听简单,半个小时应该就可以搞定,真正写的时候,画了3h+,汗颜... ...