Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split.
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
Example 3:
Input: [1,2,3,4,4,5]
Output: False
Note:
- The length of the input is in range of [1, 10000]
参考:https://discuss.leetcode.com/topic/99187/java-o-n-time-o-n-space
思路:O(N)
1. 一个数要么加到一个已经形成的序列里,要么以它为首新开辟一个3个元素序列
所有元素都可以添加到这两种序列中,那么返回true
2. 是先往已有的序列后追加, 还是先自己为首成立一个新序列?
应该是先加入已有序列中的:
如给定:前面有1 2 3 4,后面跟上5 5 6 6 7 7
是先有新序列1 2 3
再将4加入已有序列 1 2 3的后面变成 1 2 3 4
再将5加入已有序列 1 2 3 4 的后面变成 1 2 3 4 5
再有新序列5 6 7
再将6加入已有序列 1 2 3 4 5 的后面变成 1 2 3 4 5 6
再将7加入已有序列 1 2 3 4 5 6 的后面变成 1 2 3 4 5 6 7
不能是
先有新序列 1 2 3
新序列 4 5 6
新序列 5 6 7
最后一个7没地儿放
bool isPossible(vector<int>& nums)
{
unordered_map< int, int > freq; //每个key的出现次数
unordered_map< int , int > afreq;//表示有val个 key,可以和之前已有的连续序列(多个)接上
for ( auto &e : nums )
freq[e]++;
for ( auto &e : nums )
{
// the number has been used
if (freq[e] == ) continue; // the number follow after other sequence
// 表示 key,可以和 之前已有的连续序列(可能有多个) 接上
// 这里是优先把key放到 一个已有的连续序列 后,为什么?
//
else if ( afreq.find(e) != afreq.end() && afreq[e] > )
{
//将e连接到 一个已有的连续序列 之后
freq[e]--;
afreq[e]--; //下一个可以放到 上面这个已有的连续序列 后的数为 e+1(如果有)
afreq[e + ]++;
} // the number form a new sequence
// 这个序列只要 >= 3 即可,那就只判断3个元素的序列
else if ( freq.find(e + ) != freq.end() && freq[e + ] >
&& freq.find(e + ) != freq.end() && freq[e + ]>)
{
// e, e+1, e+2,组成一个新的序列
freq[e]--;
freq[e + ]--;
freq[e + ]--;
// e+3(如果有)可以连接到上面这个新的序列之后
afreq[e + ]++;
} // can't deal with this number
//既不能放到之前已有的连续序列之后,又不能组成一个新的队列
else return false; }
return true;
}

Split Array into Consecutive Subsequences的更多相关文章
- leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- [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 ...
- LeetCode Split Array into Consecutive Subsequences
原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...
- 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 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [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 ...
随机推荐
- 【371】Twitter 分类相关
Bag-of-words model:就是将句子打散成单词的集合. N-gram model:同上,只是按照 n 进行顺序组合. 参考:机器学习实战教程(四):朴素贝叶斯基础篇之言论过滤器 留言板侮辱 ...
- [原创]HTML 用div模拟select下拉框
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML xmlns=" ...
- 为了显示此页面,Firefox 必须发送将重复此前动作的数据(例如搜索或者下订单)
火狐浏览器重新加载页面出现下面提示,必须要点击“重新发送”才能完成刚才的请求: 这个是火狐的一个提示功能主要是防止同一页面的数据反复提交: 解决方法: 不要用location.reload();来刷新 ...
- VC++ 中ListCtrl经验总结
先注明一下,这里,我们用m_listctrl来表示一个CListCtrl的类对象,然后这里我们的ListCtrl都是report形式,至于其他的如什么大图标,小图标的暂时不讲,毕竟report是大众话 ...
- ESET官方下载地址
官方远程下载 ESET NOD32 Antivirus(32位) ESET NOD32 Antivirus(64位) ESET Smart Security(32位) ESET Smart Secur ...
- SPSS-生存分析
生存分析 定义:一些医学事件所经历的时间:从开始观察到事件发生的时间,不是短期内可以明确判断的.针对这类生存资料的分析方法叫生存分析.生存分析的基本概念1.终点事件终点事件outcome event: ...
- akka之种子节点
AKKA提供的cluser功能能够很便捷的创建一个分布式应用,在使用cluster时需要配置seed nodes节点,这里对seed nodes节点做一下介绍. AKKA seed nodes 和普通 ...
- window上安装 MongoDB 及其 PHP扩展
window上安装 MongoDB 及其 PHP扩展 工具/原料 window MongoDB MongoDB 方法/步骤 MongoDB 下载 MongoDB提供了可用于32位和64 ...
- vue 解决页面闪烁问题
watch: { //监听表格数据的变化[使用 watch+nextTick 可以完成页面数据监听的 不会出现闪烁] tableData: { //深度监听,可监听到对象.数组的变化 handler( ...
- TF-IDF原理及使用
1.TF-IDF(Term Frequency-Inverse Document Frequency, 词频-逆文件频率).(逆文档词频) 2.自我理解: 公式TF = $$\frac{语料库中关键词 ...