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的更多相关文章

  1. [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  2. leetcode659. Split Array into Consecutive Subsequences

    leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...

  3. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  4. Split Array into Consecutive Subsequences

    659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...

  5. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  6. [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  7. leetcode 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  8. LeetCode Split Array into Consecutive Subsequences

    原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...

  9. 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

随机推荐

  1. Typecho博客让文章列表页只显示摘要的方法

    在当前主题的 index.php 文件中找到代码 <?php $this->content('阅读剩余部分...'); ?> 将其替换为 <?php $this->exc ...

  2. win10安装后耳机有声音而外放无声音

    安装win10后耳机声音正常,而外放没声音.检查了小喇叭.播放器和设备管理器一切正常,驱动也重装了好些次.喇叭音量设置.视频音量设置均正常.花费很多时间,结果发现是键盘上有个小喇叭+的键的问题.按fn ...

  3. Integer类之缓存

    在开始详细的说明问题之前,我们先看一段代码 1 public static void compare1(){ 2 Integer i1 = 127, i2 = 127, i3 = 128, i4 = ...

  4. Atcoder Tenka1 Programmer Contest 2019 D Three Colors

    题意: 有\(n\)个石头,每个石头有权值,可以给它们染'R', 'G', 'B'三种颜色,如下定义一种染色方案为合法方案: 所有石头都染上了一种颜色 令\(R, G, B\)为染了'R', 染了'G ...

  5. Object-C-属性参数

    assign:默认参数setter 方法不会引起引用计数的变化 retain:setter方法首先释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的引用计数为1 copy:setter 方法首先 ...

  6. 使用IDEA 搭建SpringMVC +Easyui 实现最简单的数据展示功能

    效果图如下: 步骤如下: 1.导入jquery-easyui-1.5.5.6 2.导入相关的SpringMVC 的jar 包 3.编写datagrid.jsp 页面 <%-- Created b ...

  7. 2018-2019-2 20165209 《网络对抗技术》Exp4:恶意代码分析

    2018-2019-2 20165209 <网络对抗技术>Exp4:恶意代码分析 1 基础问题回答和实验内容 1.1基础问题回答 如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监 ...

  8. java copy 文件夹

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  9. C++面向对象高级开发课程(第三周)

    一,类与类之间的关系:继承(Inheritance).复合(Composition).委托(Delegation). 二,复合:表示 is-a ,该设计思想可以参照C语言的 struct . 1. 例 ...

  10. 编译错误 error C2451: “std::_Unforced”类型的条件表达式是非法的

    part 1 编译器 vs2015 VC++. 完整的错误信息粘贴如下: d:\program files (x86)\microsoft visual studio 14.0\vc\include\ ...