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. STA分析(二) multi_cycle and false

    multicycle path:当FF之间的组合逻辑path propagate delay大于一个时钟cycle时,这条combinational path能被称为multicycle path. ...

  2. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(二)——Struts2集成

    1. pom.xml文件添struts2依赖jar包: <!-- 与Struts2集成必须使用 --> <dependency> <groupId>org.spri ...

  3. Azkaban-开源任务调度程序(使用篇)

    上篇文章说到了安装,这次说说使用 登录 https://localhost:8443 注意是https,采用的是jetty ssl链接.输入账号密码azkaban/azkanban(如果你之前没有更改 ...

  4. java多线程----JUC集合”01之 框架

    java集合的架构.主体内容包括Collection集合和Map类:而Collection集合又可以划分为List(队列)和Set(集合). 1. List的实现类主要有: LinkedList, A ...

  5. c/c++日期时间处理与字符串string转换

    转自:https://www.cnblogs.com/renjiashuo/p/6913668.html 在c/c++实际问题的编程中,我们经常会用到日期与时间的格式,在算法运行中,通常将时间转化为i ...

  6. Low-level Native Plugin Interface

    http://docs.manew.com/Manual/index.htm https://docs.unity3d.com/Manual/NativePluginInterface.html ht ...

  7. JavaScript 方法扩展

    一.String全部替换方法 String.prototype.replaceAll = function(s1, s2){ return this.replace(new RegExp(s1, &q ...

  8. c++中对应java ShutdownHook的退出处理器

    最近学习cpp(至于为什么,可参考http://www.cnblogs.com/zhjh256/p/6321972.html),c++标准库第二版5.8.2节的时候,发现c++有一个对应java Sh ...

  9. django multidb --- router

    之前一篇随笔, 提到了django中怎么使用多数据库, 但是在实际工程中遇到了一个问题,就是admin指定了使用某库, 在测试环境上没问题, 当部署后(库也变动了位置), 修改一个admin的mode ...

  10. C++设计模式 之 “数据结构” 模式:Composite、Iterator、Chain of Resposibility

    "数据结构"模式 常常有一些组件在内部具有特定的数据结构,如果让客户程序依赖这些特定的数据结构,将极大地破坏组件的复用.这时候,将这些特定数据结构封装在内部,在外部提供统一的接口, ...