将数组划分成连续子序列 Split Array into Consecutive Subsequences
2018-08-04 20:47:43
问题描述:

问题描述:
本题需要的是将一个数组划分成子序列,保证每个子序列是连续的,并且长度要大于等于3。
解题思路是使用贪心算法,首先对数组中的数字进行计数,然后遍历数组,对每个数字,如果说candidate中有这个数字,那么意味着它可以和之前的子序列组成更长的序列,直接将之添加到先前的子序列中即可。如果说candidate中没有当前的数字,那么当前的数字只能作为序列的首数字出现。如果这两个都不满足,那么直接判false。
public boolean isPossible(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
Map<Integer, Integer> candidate = new HashMap<>();
for (int i : nums) map.put(i, map.getOrDefault(i, 0) + 1);
for (int i = 0; i < nums.length; i++) {
if (map.get(nums[i]) == 0) continue;
if (candidate.getOrDefault(nums[i], 0) > 0) {
candidate.put(nums[i], candidate.get(nums[i]) - 1);
candidate.put(nums[i] + 1, candidate.getOrDefault(nums[i] + 1, 0) + 1);
}
else if (map.getOrDefault(nums[i] + 1, 0) > 0 && map.getOrDefault(nums[i] + 2, 0) > 0) {
map.put(nums[i] + 1, map.get(nums[i] + 1) - 1);
map.put(nums[i] + 2, map.get(nums[i] + 2) - 1);
candidate.put(nums[i] + 3, candidate.getOrDefault(nums[i] + 3, 0) + 1);
}
else return false;
map.put(nums[i], map.get(nums[i]) - 1);
}
return true;
}
将数组划分成连续子序列 Split Array into Consecutive Subsequences的更多相关文章
- [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...
- [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- leetcode 659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- LeetCode Split Array into Consecutive Subsequences
原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...
- 659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
随机推荐
- SV中的随机化
SV搭建testbench的关键概念:CRT(constraint random test),测试集的随机化. 由于对象class由数据和操作组成,所以对数据的随机化一般放在一个class内.(对环境 ...
- linux常用命令:ss 命令
ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...
- eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined.
eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined. eclipse里error报错解决办法 ...
- selenium自动化定位方法
用selenium操作浏览器进行自动化操作其实就是通过元素属性执行相关操作.所以,我们要知道怎样去查找元素,定位元素. 常见的定位属性有: #查找元素的id find_elements_by_id(i ...
- 如何向GLSL中传入多个纹理
http://blog.csdn.net/huawenguang/article/details/41245871 如何向GLSL中传入多个纹理 这几天在研究如何实现用GLSL对多个纹理进行融合处理, ...
- ESOURCE_LOCKED - cannot obtain exclusive access to locked queue '2484_0_00163'
早上一运维同事说,一个报盘程序启动的时候报了"ESOURCE_LOCKED - cannot obtain exclusive access to locked queue '2484_0_ ...
- Docker搭建RabbitMQ集群
Docker搭建RabbitMQ集群 Docker安装 见官网 RabbitMQ镜像下载及配置 见此博文 集群搭建 首先,我们需要启动运行RabbitMQ docker run -d --hostna ...
- 利用脚本kill掉进程, 语法:运行脚本+进程名
下面附上脚本, 权限需要附X执行 #!/bin/sh #pid kill thread for chenglee #if fileformat=dos, update fileformat=unix ...
- oracle、Mysql数据库客户端DbVisualizer安装
原文链接:https://jingyan.baidu.com/article/454316ab675302f7a7c03a9e.html
- 20145305 《网络对抗》逆向及Bof基础实践
逆向及Bof基础实践说明 实践说明地址 实验过程及结果截图