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

题目:

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,

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]

题解:

对于当前distinct数字 cur, 保存以它结尾的长度1, 2, 和>=3的subsequences数目. count是cur的frequency.

当前个distinct值pre, 如果pre+1 != cur, 说明cur只能用来起头新的subsequence, 但如果前面的噗p1或p2不为0, 说明前面的只能组成长度为1或2的subsequence, return false.

如果pre+1 == cur, 说明cur可以用来append前面的subsequence.

前面长度为1的subsequence个数就是现在长度为2的subsequence个数. c2=p1.

前面长度为2的subsequence个数就是现在长度为3的subsequence个数. 若前面有长度大于3的, 这里可以继续append组成长度大于4的. 但前提是先满足前面长度1和2后剩下的. c3 = p2 + Math.min(p3, count-(p1+p2)).

也可以开始组成新的长度为1的subsequence, 前提是前面用剩下的. c1 = Math.max(0, count-(p1+p2+p3)).

Time Complexity: O(nums.length). Space: O(1).

AC Java:

 class Solution {
public boolean isPossible(int[] nums) {
int pre = Integer.MIN_VALUE;
int p1 = 0;
int p2 = 0;
int p3 = 0;
int cur = 0;
int c1 = 0;
int c2 = 0;
int c3 = 0;
int count = 0; int i = 0;
while(i<nums.length){
for(cur = nums[i], count = 0; i<nums.length && cur==nums[i]; i++){
count++;
} if(cur != pre+1){
if(p1!=0 || p2!=0){
return false;
} c1 = count;
c2 = 0;
c3 = 0;
}else{
if(count < p1+p2){
return false;
} c1 = Math.max(0, count-(p1+p2+p3));
c2 = p1;
c3 = p2 + Math.min(p3, count-(p1+p2));
} pre=cur;
p1=c1;
p2=c2;
p3=c3;
}
return p1==0 && p2==0;
}
}

如果给出的nums不是sorted的, 则可先统计数字的frequency.

再扫一遍nums array, 对于每个数字要么能承上, 要么能启下.

都不能就return false.

Time Complexity: O(nums.length).

Space: O(nums.length).

AC Java:

 class Solution {
public boolean isPossible(int[] nums) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int num : nums){
hm.put(num, hm.getOrDefault(num, 0)+1);
} HashMap<Integer, Integer> append = new HashMap<Integer, Integer>();
for(int num : nums){
if(hm.get(num) == 0){
continue;
} if(append.getOrDefault(num, 0) > 0){
append.put(num, append.get(num)-1);
append.put(num+1, append.getOrDefault(num+1, 0)+1);
}else if(hm.getOrDefault(num+1, 0) > 0 && hm.getOrDefault(num+2, 0) > 0){
hm.put(num+1, hm.get(num+1)-1);
hm.put(num+2, hm.get(num+2)-1);
append.put(num+3, append.getOrDefault(num+3, 0)+1);
}else{
return false;
} hm.put(num, hm.get(num)-1);
} return true;
}
}

LeetCode Split Array into Consecutive Subsequences的更多相关文章

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

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

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

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

  3. Split Array into Consecutive Subsequences

    659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...

  4. leetcode659. Split Array into Consecutive Subsequences

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

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

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

  6. leetcode 659. Split Array into Consecutive Subsequences

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

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

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

  8. 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. Linux静默安装Oracle

    打算在云服务器上装oracle服务,以前DBA美眉都是在图形化界面下安装,这次抓瞎了.赶紧上网查查,静默安装可以解决问题.于是乎赶紧开始部署,过程如下.安装环境:操作系统:CentOS 7内存:11G ...

  2. Apache 浏览器访问限制配置

    浏览器访问限制配置 user_agent收入的浏览器中,我们通过百度,谷歌很容易就可以查到相关的一些资料,方便了我们对知识的查找,但在某些特定情况下,我们并不希望有人可以通过某写搜索引擎直接访问到我们 ...

  3. Android Studio 入门级教程

    引用原文:http://www.cnblogs.com/abao0/p/6934023.html 写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权.希望尽自己的努力 ...

  4. CentOS 7 安装各个桌面版本

    http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7 92dow ...

  5. scala mapPartitionsWithIndex函数的使用

    var rdd1=sc.makeRDD(Array((1,"A"),(2,"B"),(3,"C"),(4,"D")),2 ...

  6. TED字幕摘抄

    1.丹·吉尔伯特: 我们为什么快乐?http://v.163.com/movie/2012/12/0/S/M8HHB6LDT_M8HHCBM0S.html 在两百万年中, 大脑脑容量从我们祖先能人的1 ...

  7. cassandra 之 集群部署

    其实关于部署没啥好说的,修改config/cassandra.yaml以下几个地方就可以了 cluster_name: 'cluster_cassandra' data_file_directorie ...

  8. Eclipse的编码格式设置

    在使用Eclipse开发中,许多文件编码默认是ISO-8859-1,不支持中文(如常用的JSP).这样我们每次建文件都要手动改编码,其实我们可以在设置文件默认编码,今后再创建时就不用每次修改编码了, ...

  9. Spring之RMI 远程方法调用 (Remote Method Invocation)

    RMI 指的是远程方法调用 (Remote Method Invocation) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程 ...

  10. redis、kafka、rabittMQ对比 (转)

    本文不对三者之间的性能进行对比,只是从三者的特性上区分他们,并指出三者的不用应用场景. 1.publish/subscribe 发布订阅模式如下图所示可以具有多个生产者和发布者,redis.kafka ...