原题链接在这里: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:

  1. Input: nums = [3, 4, 2]
  2. Output: 6
  3. Explanation:
  4. Delete 4 to earn 4 points, consequently 3 is also deleted.
  5. Then, delete 2 to earn 2 points. 6 total points are earned.

Example 2:

  1. Input: nums = [2, 2, 3, 3, 3, 4]
  2. Output: 9
  3. Explanation:
  4. Delete 3 to earn 3 points, deleting both 2's and the 4.
  5. Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
  6. 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].

题解:

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:

  1. class Solution {
  2. public int deleteAndEarn(int[] nums) {
  3. if(nums == null || nums.length == 0){
  4. return 0;
  5. }
  6.  
  7. int n = 10001;
  8. int [] buckets = new int[n];
  9. for(int num : nums){
  10. buckets[num]++;
  11. }
  12.  
  13. int in = 0;
  14. int ex = 0;
  15. for(int i = 0; i<n; i++){
  16. int inclusive = ex + i*buckets[i];
  17. int exclusive = Math.max(in, ex);
  18. in = inclusive;
  19. ex = exclusive;
  20. }
  21.  
  22. return Math.max(in, ex);
  23. }
  24. }

类似House Robber.

LeetCode 740. Delete and Earn的更多相关文章

  1. LC 740. Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  2. leetcode笔记(六)740. Delete and Earn

    题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...

  3. 【leetcode】740. Delete and Earn

    题目如下: Given an array nums of integers, you can perform operations on the array. In each operation, y ...

  4. 740. Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  5. [LeetCode]Delete and Earn题解(动态规划)

    Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...

  6. [LeetCode] Delete and Earn 删除与赚取

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  7. [Swift]LeetCode740. 删除与获得点数 | Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  8. [LeetCode] Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  9. [LeetCode] Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

随机推荐

  1. c++11多线程记录6:条件变量(condition variables)

    https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...

  2. 不一样的go语言-玩转语法之一

      这段时间为俗事所累,疲以应付,落下了不少想法,错过了更新的日子.这个专题开始之际,已经准备了不下十几个主题,而在写作的过程中,又有新想法与主题涌现出来.未来预计想写写的内容主要包括: 玩转语法系列 ...

  3. (五)pdf的构成之文件体(catalog对象)

    引自:https://blog.csdn.net/steve_cui/article/details/82735039 目录(catalog): 文档目录包含对定义文档内容的其他对象的引用.它还包含声 ...

  4. XML和Json的特点

    Xml特点: 1.有且只有一个根节点: 2.数据传输的载体 3.所有的标签都需要自定义 4.是纯文本文件 Json(JavaScript Object Notation)特点: json分为两种格式: ...

  5. Python进阶----UDP协议使用socket通信,socketserver模块实现并发

    Python进阶----UDP协议使用socket通信,socketserver模块实现并发 一丶基于UDP协议的socket 实现UDP协议传输数据 代码如下:

  6. Falsk框架 Session 与 Flask-Session

    目录 Cookie 与 Session 简单了解 Falsk 中 Session 的保管机制 相关的配置 使用 Flask-Session 三方组件 基础练习题 Cookie 与 Session 简单 ...

  7. java Document生成和解析xml

    转自:https://blog.csdn.net/p812438109/article/details/81807440 Document场景:需要知道文档所有结构 需要把文档一些元素排序 文档中的信 ...

  8. Android存储及getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()区别

    存储介绍 Android系统分为内部存储和外部存储,内部存储是手机系统自带的存储,一般空间都比较小,外部存储一般是SD卡的存储,空间一般都比较大,但不一定可用或者剩余空间可能不足.一般我们存储内容都会 ...

  9. 教你如何配置linux用户实现禁止ssh登陆机器但可用sftp登录!

    构想和目标最近有个这样的诉求:基于对线上服务器的保密和安全,不希望开发人员直接登录线上服务器,因为登录服务器的权限太多难以管控,如直接修改代码.系统配置,并且也直接连上mysql.因此希望能限制开发人 ...

  10. Android笔记(七十六) 点菜DEMO

    一个朋友让看一下他的代码,一个点菜的功能,他和我一样,初学者,代码比我的都混乱,也是醉了,干脆想着自己写个demo给他看,原本想着听简单,半个小时应该就可以搞定,真正写的时候,画了3h+,汗颜... ...