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 ...
随机推荐
- nginx通过自定义header属性来转发不同的服务
一.背景 因为需要上线灰度发布,只要nginx接收到头部为: wx_unionid: 就会跳转到另外一个url,比如: 通过配置nginx 匹配请求头wx_unionid 来转发到灰度环境.核心:客户 ...
- windows 10 如何设定计划任务自动执行 python 脚本?
我用 python 写了一些脚本,有一些是爬虫脚本,比如爬取知乎特定话题的热门问题,有一些是定期的统计分析脚本,输出统计结果到文档中.之前我都是手动执行这些脚本,现在我希望如何这些脚本能自动定时执行. ...
- Angular—入门环境,项目创建,导入项目
1.安装 nodejs,npm nodejs 下载地址: https://nodejs.org/en/download/
- MVC学习笔记(二)—用EF创建数据库
1.创建一个空项目 2.在项目中创建EFCore的类库 3.在NuGut控制台为EFCore项目中安装entity 3.1 命令为:Install-Package EntityFramework(在 ...
- Source roots (or source folders) Test source roots (or test source folders; shown as rootTest)Resource rootsTest resource roots
idea中Mark Directory As里的Sources Root.ReSources Root等的区别 1.Source roots (or source folders) 通过这个类指定一个 ...
- 07、MySQL—时间日期类型
时间日期类型 1.Date 日期类型:系统使用三个字节来存储数据,对应的格式为:YYYY-mm-dd,能表示的范围是从1000-01-01 到9999-12-12,初始值为0000-00-00 2.T ...
- React Native 开发豆瓣评分(七)首页组件开发
首页内容拆分 看效果图,首页由热门影院.豆瓣热门.热门影视等列表组成,每个列表又由头加横向滑动的 电影海报列表构成. 所以可以先把页面的电影海报.评分.列表头做成组件,然后在使用 ScrollView ...
- Oracle 用户与模式的关系
一.用户登录 Oracle 数据库的安全保护流程分为3个步骤. 1.用户向数据库提供身份识别信息,即提供一个数据库账号 2.证明身份识别信息是否有效,通过密码来实现 3.用户输入的密码经过数据库的核对 ...
- Eclipse - 安装Indent Guide,使JAVA代码具备编辑缩进线,和Delphi一样酷!!
Delphi里面默认就有代码编辑缩进线,这在编写逻辑复杂的大块代码时候非常有好处,可以清楚地看到每块代码的范围,下面就是Delphi里面代码编辑缩进线的截图: 非常棒的效果,我喜欢,在处理复杂逻辑代码 ...
- 物料管理混乱怎么办?APS系统帮你实现高效运输
APS系统可以高效地管理.控制分销中心并保证产品可订货.可盈利.能力可用.分销计划帮助企业分析原始信息,然后企业能够确定如何优化分销成本或者根据生产能力和成本提高客户服务水平. 今天成功的企业为了取得 ...