原题链接在这里: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. Python面试题之容器(Collections)

    容器(Collections) Python附带一个模块,它包含许多容器数据类型,名字叫作collections.我们将讨论它的作用和用法.   我们将讨论的是:   defaultdict coun ...

  2. AndroidDevTools下载

    收集整理Android开发所需的Android SDK.开发中用到的工具.Android开发教程.Android设计规范,免费的设计素材等. http://www.androiddevtools.cn ...

  3. Autofac is designed to track and dispose of resources for you.

    https://autofaccn.readthedocs.io/en/latest/best-practices/ Autofac is designed to track and dispose ...

  4. cmd常用命令大全

    cmd命令提示符:只是系统模拟的dos操作环境,且功能远远大于dos 1. 返回上一层 cd.. 2. 进入A文件夹  cd A 3. 进入A文件夹下的B文件夹   cd  A/B 4. c盘下的A文 ...

  5. windows系统下载地址大全&大白菜下载和教程

    win10的 Windows10 64位纯净系统下载(不建议,后面的有原版) http://cjxt.sysdaa.com/down.php?post=win10-64&action=bend ...

  6. thrift安装及常见问题

    一.安装thrift (macOS / Linux) 1. 下载thrift0.10.0源码 https://github.com/apache/thrift/releases/tag/0.10.0 ...

  7. MySQL5.7版本开启二进制日志是log_bin、bin-log 还是 bin_log ?

    已Mac系统为例,文件:/usr/local/mysql/support-files/my-default.cnf 是mysql的默认配置文件,你可以直接修改这个文件但是不推荐,你可以在/etc/my ...

  8. Node.js小白开路(一)-- fs篇

    文件操作在我们的日常功能模块之中是十分的常见的内容,nodeJS也不例外的为我们提供了之一操作内容,当时在我们了解文件操作的之前我们先来了解一下链接. 连接可以理解成为一个纸箱相关文件内容的地址,其主 ...

  9. 伪变量foo foober是什么意思

    原文: The terms foobar, foo, bar, baz and qux are sometimes used as placeholder names (also referred t ...

  10. Windows系统下MySQL解压版添加到系统服务

    MySQL软件版本:64位 5.7.12 1.首先配置MySQL的环境变量,在系统环境变量Path的开头添加MySQL的bin目录的路径,以“;”结束,我的路径配置如下: 2.修改MySQL根目录下的 ...