LeetCode Split Array into Consecutive Subsequences
原题链接在这里: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:
- 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的更多相关文章
- [LeetCode] 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 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...
- leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...
- [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 ...
- [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- 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 ...
随机推荐
- FTP vsftp 安装、管理
FTP简介 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为文传协议,用户Internet上的控制文件的双向传输. FTP的主要作用,就是让用户链接上一个远 ...
- dubbo应用
一.安装配置 cd /usr/local/ wget http://www.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar. ...
- React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发
React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发 2016/09/23 | React Native技术文章 | Sky丶清| 4 条评论 | 1 ...
- JAVA基础补漏--static
静态方法不能访问非静态变量的原因 静态的方法和变量在内存中先产生,非静态的后产生,在静态调用时非静态可能还未创建,所以会发生错误,故不能访问. static的内存图 静态代码块 static { Sy ...
- Hardware Prefetcher
硬件预取选项,指CPU有硬件预取功能,在CPU处理指令或数据之前,它将这些指令或数据从内存预取到L2缓存中,借此减少内存读取的时间,帮助消除潜在的瓶颈,以此提高系统效能.通常情况下建议设置为Enabl ...
- spring的几个通知(前置、后置、环绕、异常、最终)
1.没有异常的 2.有异常的 1.被代理类接口Person.java package com.xiaostudy; /** * @desc 被代理类接口 * * @author xiaostudy * ...
- DFS - 深度搜索 - 基于邻接列表表示法
2017-07-25 15:38:00 writer:pprp 在前一篇图基于邻接列表表示法的代码加了一小部分,加了一个DFS函数,visited[N]数组 参考书目:张新华的<算法竞赛宝典&g ...
- 委托,lambda,匿名方法
lambda表达式其实就是匿名方法的变体或者说简写. 原来我们用 delegate void Del(int x); Del d = delegate(int x) { return x + 1; } ...
- Git GUI 的使用
下面,我们开始使用Git Gui 如果你想init一个本地的git仓库,到你的代码根目录下,右键选择Git Init Here 这时,你会发现在代码根目录下,生成了一个.git的隐藏属性目录. 再选择 ...
- 遍历jsonArray和jsonObject
遍历jsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}]&q ...