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:

  1. 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的更多相关文章

  1. leetcode659. Split Array into Consecutive Subsequences

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

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

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

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

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

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

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

  5. LeetCode Split Array into Consecutive Subsequences

    原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...

  6. 659. Split Array into Consecutive Subsequences

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

  7. leetcode 659. Split Array into Consecutive Subsequences

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

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

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

  9. [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 ...

随机推荐

  1. Android 操作UI线程的一些方法

    我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据.但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的.下面总结4 ...

  2. week05 05restful api

    和第一个项目一样 然后去App.js注册一下 但是呢 新闻是写死在 现在主要输调通前端client和后端server 持续获取新闻 至于真假先不考虑 下面我们回到前端NewsPanel 这个reque ...

  3. UploadFtp

    #!/bin/bash FILENAME=$ DSTDIR=$ FTPSRV=ip FTPUSER="user" FTPPWD="password" SRCDI ...

  4. socket编程的同步、异步与阻塞、非阻塞示例详解

     socket编程的同步.异步与阻塞.非阻塞示例详解之一  分类: 架构设计与优化 简介图 1. 基本 Linux I/O 模型的简单矩阵 每个 I/O 模型都有自己的使用模式,它们对于特定的应用程序 ...

  5. arcgis_SDE安装步骤

    弄了将近一个星期的Oracle和ArcSDE终于让我给弄好了!下面把过程跟大家分享一下: 首先是Oracle10gR2的安装,在Oracle的官方网站上可以下到Oracle10gR2的安装程序,安装过 ...

  6. 使用qt creator4.XXX,b编辑和调试caffe,太好用了

    一直想看caffe的源代码,网上看了一个qt的例子,但是自己也有qt creator,怎么就不行 后面发现是自己的版本太低所以不好用(可能是自己能力有限) 可以参考下面这个链接: 使用qt creat ...

  7. kibana test

    https://www.cnblogs.com/yiwangzhibujian/p/7137546.html curl -XPUT http://localhost:9200/shakespeare ...

  8. C++访问二维数组元素

    if(*image_in+j*+xsize+i)>=thresh)//xsize图像宽度 image_out是首地址,加上j*行宽就是目标行的首地址,再加上i,就是在此行中的第i个像素,所以整个 ...

  9. Webpack Getting Started

    [Webpack Getting Started] Make sure you have a fresh version of Node.js installed. If you are using ...

  10. vue 学习1

    .static{ border-radius:4px; } .active { width: 100px; height: 100px; background: green; } .text-dang ...