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. 阅读笔记--java内存模型原理

    在阅读本篇文章之前,我所理解的和上网了解到的java内存模型原理如下: 不同架构的物理计算机可以有不一样的内存模型,Java 虚拟机也有自己的内存模型.Java 虚拟机规范中试图定义一种 Java 内 ...

  2. shell教程<入门篇>

    由于我平时的工作环境是linux,所以无可避免的经常使用命令行模式和shell脚本,而且有些命令行每天都要输好多遍,比如ssh登录之类的,所以干脆把平时常用的命令都写成脚本文件,所以特意开了一个she ...

  3. Redis高级用法

    第一章 redis初识 1.1 Redis是什么 介绍 开源:早起版本2w3千行 基于键值对的存储系统:字典形式 多种数据结构:字符串,hash,列表,集合,有序集合 高性能,功能丰富 那些公司在用 ...

  4. HDU 5423:Rikka with Tree Dijkstra算法

    Rikka with Tree  Accepts: 207  Submissions: 815  Time Limit: 2000/1000 MS (Java/Others)  Memory Limi ...

  5. python数组定义

    转自:https://blog.csdn.net/minsenwu/article/details/7872679 Python中没有数组的数据结构,但列表很像数组,如: a=[0,1,2],这时a[ ...

  6. Maven:Eclipse导入从SVN上检出的Maven多模块工程

    大致步骤: 1.从SVN中检出多模块项目,名称随意(Eclipse中可以在[Window ==>>Show View==>>Other==>>SVN==>&g ...

  7. 未能加载文件或程序集“MySql.Data”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    未能加载文件或程序集“MySql.Data”或它的某一个依赖项.找到的程序集清单定义与程序集引用不匹配. (异常来自 HRESULT:0x80131040) 说明: 执行当前 Web 请求期间,出现未 ...

  8. ABP JTable如何显示序列号

    显示序列号 recordsLoaded: function (event, data) { var SrNo = 0; if (data.records) { $.each(data.records, ...

  9. 小程序调用wx.chooseLocation接口的时候无法获取权限(ios)

    ios手机小程序调用wx.chooseLocation接口的时候,获取权限的时候报authorize:fail:require permission desc这样子的错误,这是由于苹果的安全机制导致需 ...

  10. ci框架与smarty的整合

    ci框架与smarty的整合 来源:未知    时间:2014-10-20 11:38   阅读数:108   作者:xbdadmin [导读] Ci 和 smarty 的完美结合 Ci 结合 sma ...