Given an array nums sorted in ascending order, return true if and only if you can split it into 1 or more subsequences such that each subsequence consists of consecutive integers and has length at least 3.

Example 1:

Input: [1,2,3,3,4,5]
Output: True
Explanation:
You can split them into two consecutive subsequences :
1, 2, 3
3, 4, 5

Example 2:

Input: [1,2,3,3,4,4,5,5]
Output: True
Explanation:
You can split them into two consecutive subsequences :
1, 2, 3, 4, 5
3, 4, 5
class Solution {
public boolean isPossible(int[] nums) {
Map<Integer, Integer> freqMap = new HashMap<>();
Map<Integer, Integer> hypoMap = new HashMap<>();
for (int num: nums) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
} for (int num : nums) {
// base case
if (freqMap.get(num) == 0) {
continue;
}
if (hypoMap.getOrDefault(num, 0) > 0) {
hypoMap.put(num, hypoMap.get(num) - 1);
freqMap.put(num, freqMap.get(num) - 1);
hypoMap.put(num + 1, hypoMap.getOrDefault(num + 1, 0) + 1);
} else if (freqMap.getOrDefault(num, 0) > 0 && freqMap.getOrDefault(num + 1, 0) > 0 && freqMap.getOrDefault(num + 2, 0) > 0) {
freqMap.put(num, freqMap.get(num) - 1);
freqMap.put(num + 1, freqMap.get(num + 1) - 1);
freqMap.put(num + 2, freqMap.get(num + 2) - 1);
hypoMap.put(num + 3, hypoMap.getOrDefault(num + 3, 0) + 1);
} else {
return false;
}
}
return true;
}
}

[LC] 659. Split Array into Consecutive Subsequences的更多相关文章

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

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

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

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

  3. 659. Split Array into Consecutive Subsequences

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

  4. leetcode 659. Split Array into Consecutive Subsequences

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

  5. 【leetcode】659. Split Array into Consecutive Subsequences

    题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为 ...

  6. Split Array into Consecutive Subsequences

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

  7. leetcode659. Split Array into Consecutive Subsequences

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

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

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

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

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

随机推荐

  1. (递归)Hanoi Tower

    #include<stdio.h>void move(int n,char a,char b){ printf("将第%d个盘子从%c移动到%c\n",n,a,b);  ...

  2. 201771010123汪慧和《面向对象程序设计Java》第十五周实验总结

    一.理论部分 1.JAR文件 (1)Java程序的打包:程序编译完成后,程序员将.class文件压缩打包为.jar文件后,GUI界面 程序就可以直接双击图标运行. (2).jar文件(Java归档)既 ...

  3. 学习Github必须要会的知识

    目的     托管项目代码 基本概念 仓库Respository   存放项目代码,每个项目对应一个仓库,多个开源项目则有多个仓库. 收藏Star   收藏项目,方便下次查看. 复制克隆项目Fork ...

  4. freeswitch初识

    一.参考内容 1.<FreeSWITCH权威指南>杜金房.张令考著 2.FreeSwitch 国内源码快速下载 3.Ubuntu下freeSwitch安裝指导(推荐) 4.Freeswit ...

  5. Python中的常用内置对象之map对象

    如果你了解云计算的最重要的计算框架Mapreduce,你就对Python提供的map和reduce对象有很好的理解,在大数据面前,单机计算愈加力不从心,分布式计算也就是后来的云计算的框架担当大任,它提 ...

  6. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  7. 安装adobe reader阅读器

    首先  在我的网盘里有那个软件. 安装的教程在这个歌网址:http://www.zhanshaoyi.com/6730.html

  8. UML-UML工具与UML蓝图

    1.UML应用场景 1).UML作为草图 2).UML作为蓝图. UML生成java代码(前向工程) java代码生成UML(逆向工程) 2.如果绘制了UML草图,如何在编码后更新该图形? 逆向工程, ...

  9. mui 上拉加载

    最近做到移动端页面的开发,需要mui 的上拉刷新功能,最后实现后整理代码如下: 1.需要引入的js <link href="../resource/css/mui.min.css&qu ...

  10. OpenCV和Qt的图像格式互转

    做图像处理的时候经常使需要用到opencv的,这应该是免费的图像处理库中用得最广泛而且最好用的库了吧.然后有时候想用界面来展示点东西的时候,我们就需要编写个界面,编写界面的方法千千万,弱水三千我只取一 ...