[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 ...
随机推荐
- 51nod 1267:4个数和为0 哈希
1267 4个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出&qu ...
- nginx log 切割
/logs/nginx/*/*access.log { daily rotate 30 missingok dateext #compress notifempty sharedscripts pos ...
- Linux基础应用
Linux刚面世时并没有图形界面,因此所有的操作全靠命令完成,如磁盘操作.文件读取.目录操作.进程管理.文件权限等都要通过命令完成.且在职场中,大量的服务器维护都是通过远程命令来完成. 常用的7个命令 ...
- 修改默认SQL字符集
Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=MSSQLSERVER /SQLSYSADMINACCOUNTS=lab\sccmadmin /S ...
- php魔术常量,_CLASS_,_METHOD_,_FUNCTION_
_CLASS_: 返回当前类的类名 _METHOD_:返回当前类方法的方法名(并显示类的调用,类名::方法名) _FUNCTION_:返回当前函数的函数名 _FILE_:当前文件的绝对路径(包含_FI ...
- 7)给tab下面添加一个子非模态对话框
1)还是沿袭(6)那个代码 2)下面是步骤: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·~~~~~~~~~~~~~~~~~~~~~~~~~~~ 然后,修改这个对 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:函数参数
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PAT A1005-1008
A 1005 Spell It Right (20 point(s)) 25分的题目,比较简单,注意N的范围,用字符串处理即可. #include <iostream> #include ...
- 基于python的arcgis底图添加(转)
本文翻译自:Qingkai‘s Blog 当使用python的Basemap库绘制地图时,选择一个漂亮的底图会为图片增色不少,但是使用map.bluemarble().map.etopo()或者map ...
- pc页面在移动端浏览时部分字体放大,与pc字体大小不一致(Font Boosting)
最近做一个页面时,需要pc的页面在移动端浏览时保持pc的样式缩小.但是发现部分文字被放大了.看上去特别诡异.如下图 绿框是PC端查看时的正常样式,红框是移动端看的字体放大的诡异样式 是什么原因呢? 后 ...