将数组划分成连续子序列 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 ...
随机推荐
- BinarySearch
今天看代码,看到这么一段,开始没有看明白,记录下来备忘 foreach (FinancialReport r3 in addAorList) { i ...
- 《Convolutional Neural Networks for Sentence Classification》 文本分类
文本分类任务中可以利用CNN来提取句子中类似 n-gram 的关键信息. TextCNN的详细过程原理图见下: keras 代码: def convs_block(data, convs=[3, 3, ...
- typed.js
实现效果,文字逐个输出. 实例代码: <script> $(function(){ $("#head-title").typed({ strings: ["为 ...
- JavaScript: apply 方法 详解(转)——非常好
转载自 http://www.cnblogs.com/KeenLeung/archive/2012/11/19/2778229.html 我在一开始看到javascript的函数apply和call ...
- selenium3启动浏览器
selenium3比2更加标准化,支持的浏览器更多,启动浏览器需要做如下操作: 首先,下载对应浏览器的驱动,并放在项目下(也可以放在其他地方,写代码的时候路径填写正确即可),如下图所示: 下面就是启动 ...
- 在ubuntu英文系统下使用中文输入法
How to install and use Chinese Input Method in the English Locale in Ubuntu ?(1) Check if there exis ...
- 20145225 《网络对抗》逆向及Bof基础实践
实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- 0xc0000005:读取位置时发生访问冲突
这是空指针,比如: A* a=NULL; a->fun();//会提示标题错误,因为a没有分配空间
- Python3基础 yield 在函数中的用法示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3基础 try-except-finally 的简单示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...