原题链接在这里: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:

  1. Input: [1,2,3,3,4,5]
  2. Output: True
  3. Explanation:
  4. You can split them into two consecutive subsequences :
  5. 1, 2, 3
  6. 3, 4,

Example 2:

  1. Input: [1,2,3,3,4,4,5,5]
  2. Output: True
  3. Explanation:
  4. You can split them into two consecutive subsequences :
  5. 1, 2, 3, 4, 5
  6. 3, 4, 5

Example 3:

  1. Input: [1,2,3,4,4,5]
  2. 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:

  1. class Solution {
  2. public boolean isPossible(int[] nums) {
  3. int pre = Integer.MIN_VALUE;
  4. int p1 = 0;
  5. int p2 = 0;
  6. int p3 = 0;
  7. int cur = 0;
  8. int c1 = 0;
  9. int c2 = 0;
  10. int c3 = 0;
  11. int count = 0;
  12.  
  13. int i = 0;
  14. while(i<nums.length){
  15. for(cur = nums[i], count = 0; i<nums.length && cur==nums[i]; i++){
  16. count++;
  17. }
  18.  
  19. if(cur != pre+1){
  20. if(p1!=0 || p2!=0){
  21. return false;
  22. }
  23.  
  24. c1 = count;
  25. c2 = 0;
  26. c3 = 0;
  27. }else{
  28. if(count < p1+p2){
  29. return false;
  30. }
  31.  
  32. c1 = Math.max(0, count-(p1+p2+p3));
  33. c2 = p1;
  34. c3 = p2 + Math.min(p3, count-(p1+p2));
  35. }
  36.  
  37. pre=cur;
  38. p1=c1;
  39. p2=c2;
  40. p3=c3;
  41. }
  42. return p1==0 && p2==0;
  43. }
  44. }

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

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

都不能就return false.

Time Complexity: O(nums.length).

Space: O(nums.length).

AC Java:

  1. class Solution {
  2. public boolean isPossible(int[] nums) {
  3. HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
  4. for(int num : nums){
  5. hm.put(num, hm.getOrDefault(num, 0)+1);
  6. }
  7.  
  8. HashMap<Integer, Integer> append = new HashMap<Integer, Integer>();
  9. for(int num : nums){
  10. if(hm.get(num) == 0){
  11. continue;
  12. }
  13.  
  14. if(append.getOrDefault(num, 0) > 0){
  15. append.put(num, append.get(num)-1);
  16. append.put(num+1, append.getOrDefault(num+1, 0)+1);
  17. }else if(hm.getOrDefault(num+1, 0) > 0 && hm.getOrDefault(num+2, 0) > 0){
  18. hm.put(num+1, hm.get(num+1)-1);
  19. hm.put(num+2, hm.get(num+2)-1);
  20. append.put(num+3, append.getOrDefault(num+3, 0)+1);
  21. }else{
  22. return false;
  23. }
  24.  
  25. hm.put(num, hm.get(num)-1);
  26. }
  27.  
  28. return true;
  29. }
  30. }

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. FTP vsftp 安装、管理

    FTP简介 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为文传协议,用户Internet上的控制文件的双向传输. FTP的主要作用,就是让用户链接上一个远 ...

  2. dubbo应用

    一.安装配置 cd /usr/local/ wget http://www.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar. ...

  3. React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发

    React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发   2016/09/23 |  React Native技术文章 |  Sky丶清|  4 条评论 |  1 ...

  4. JAVA基础补漏--static

    静态方法不能访问非静态变量的原因 静态的方法和变量在内存中先产生,非静态的后产生,在静态调用时非静态可能还未创建,所以会发生错误,故不能访问. static的内存图 静态代码块 static { Sy ...

  5. Hardware Prefetcher

    硬件预取选项,指CPU有硬件预取功能,在CPU处理指令或数据之前,它将这些指令或数据从内存预取到L2缓存中,借此减少内存读取的时间,帮助消除潜在的瓶颈,以此提高系统效能.通常情况下建议设置为Enabl ...

  6. spring的几个通知(前置、后置、环绕、异常、最终)

    1.没有异常的 2.有异常的 1.被代理类接口Person.java package com.xiaostudy; /** * @desc 被代理类接口 * * @author xiaostudy * ...

  7. DFS - 深度搜索 - 基于邻接列表表示法

    2017-07-25 15:38:00 writer:pprp 在前一篇图基于邻接列表表示法的代码加了一小部分,加了一个DFS函数,visited[N]数组 参考书目:张新华的<算法竞赛宝典&g ...

  8. 委托,lambda,匿名方法

    lambda表达式其实就是匿名方法的变体或者说简写. 原来我们用 delegate void Del(int x); Del d = delegate(int x) { return x + 1; } ...

  9. Git GUI 的使用

    下面,我们开始使用Git Gui 如果你想init一个本地的git仓库,到你的代码根目录下,右键选择Git Init Here 这时,你会发现在代码根目录下,生成了一个.git的隐藏属性目录. 再选择 ...

  10. 遍历jsonArray和jsonObject

    遍历jsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}]&q ...