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 ...
随机推荐
- Python面试题之容器(Collections)
容器(Collections) Python附带一个模块,它包含许多容器数据类型,名字叫作collections.我们将讨论它的作用和用法. 我们将讨论的是: defaultdict coun ...
- ASP.NET MVC 在项目中使用面包屑导航
给框架添加一个面包屑导航 1.创建一个类 using System; using System.Collections.Generic; using System.Linq; using System ...
- ASP.NET MVC jQuery 树插件在项目中使用方法(一)
jsTree是一个 基于jQuery的Tree控件.支持XML,JSON,Html三种数据源.提供创建,重命名,移动,删除,拖"放节点操作.可以自己自定义创建,删 除,嵌套,重命名,选择节点 ...
- Unable to handle kernel NULL pointer dereference at virtual address 00000000【转】
本文转载自:https://blog.csdn.net/hpu11/article/details/72628052 这说明是非法指针的使用,才导致系统出错. [ 1023.510000] Unabl ...
- Which HTTP methods match up to which CRUD methods?
https://stackoverflow.com/questions/6203231/which-http-methods-match-up-to-which-crud-methods Crea ...
- bzoj 1050: [HAOI2006]旅行comf(codevs.cn 1001 舒适的路线) 快排+并查集乱搞
没用的话:好像很久没发博客了,主要是懒太蒟找不到水题.我绝对没弃坑...^_^ 还用些话:本文为博主原创文章,若转载请注明原网址和作者. 进入正题: 先pa网址: bzoj :http://www.l ...
- 文件(1)--File
File简介 Java.io.File用于表示文件(目录),也就是说程序员可以通过File类在程序中操作硬盘上的文件和目录.File类只用于表示文件(目录)的信息(名称.大小等),不能对文件的内容进行 ...
- 红米手机.驱动.XP安装
1.发现 官网上下载的 驱动在 XP下安装不上去... (Win7 记得 貌似 没有问题...) 1.1.网上搜到的 解决方案为:解决手机不能连电脑 XP系统无法安装MTP设备驱动的终极解决_小米No ...
- Educational Codeforces Round 27
期末后恢复性训练,结果完美爆炸... A,题意:2n个人,分成两队,要求无论怎么分配,第一队打赢第二队 #include<bits/stdc++.h> #define fi first # ...
- 10074 启用开发者模式 for vs2015rc
1. 关于VS2015RC 有两个版本,它们都包含了Windows 10 SDK. 社区版:免费,可以开发Windows UAP应用.iOS和Android应用.在 帮助->注册产品 菜单可以登 ...