[LC] 659. Split Array into Consecutive Subsequences
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的更多相关文章
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- 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】659. Split Array into Consecutive Subsequences
题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为 ...
- Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...
- leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...
- [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
随机推荐
- CodeForces - 748F Santa Clauses and a Soccer Championship
题意:有k对队伍,每对队伍之间将举行两次比赛,两支队伍各主办一次.住宿的地方要求在两支队伍家乡的最短路的结点上或者在两支队伍的家乡.问在选择住宿处最少的情况下,怎么组成这k对队伍? 分析: 1.因为n ...
- 远程过程调用——RPC
https://www.jianshu.com/p/5b90a4e70783 清晰明了
- ROS的Target Platforms
问题 今天编译VIORB遇到了以下错误: 查到的资料 ubuntu18安装的二进制ros包需要特定版本的库函数 ros官方文档 文档pdf 参考回答 参考回答pdf
- Tutorial: Create a Blinky ARM test project(创建一个闪灯的arm测试项目)
Background ref : Tutorial: Create a Blinky ARM test project If you are new to ARM development, it is ...
- github 新建库,提交命令
Command line instructions You can also upload existing files from your computer using the instructio ...
- JavaScript学习总结(四)
这一部分我们继续介绍JavaScript的常用对象. Number对象 创建Number对象 方式1: var 变量= new Number(数字) 方式2: var 变量 = 数字; 常用的方法 t ...
- XML技术详解
XML 1.XML概述 XML可扩展标记语言是一种基于文本的语言用作应用程序之间的通信模式,是一个非常有用的描述结构化信息的技术.XML工具使得转化和处理数据变得十分容易,但同样也要领域相关的标准和代 ...
- Java学习笔记--精品札记
forech循环增强版(JDK1.7新特性) for(数组单位元素类型 i:遍历目标数组){ 代码块 } char(只能放单个字符)数组可以直接遍历不需要循环,其他数组不可以,必须遍历 toStrin ...
- linux文件软链接与硬链接
1.命令格式: ln [参数][源文件或目录][目标文件或目录] 软链接只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间. 2.命令功能: Linux文件系统中,有所谓的链接(link),我们 ...
- 17.3.12----OS模块ya
1---他是一xu个python系统编程的操作模块,可以处理文件和目录这些我们日常手动需要做的操作. import os#导入这个os模块,其实python模块就是C元的包含很多函数的文件 2---o ...