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 ...
随机推荐
- Android Gradle 构建工具(Android Gradle Build Tools)是什么?
转载地址:http://mrfu.me/android/2015/07/17/New_Android_Gradle_Build_Tools/ 译者地址:[翻]一览新的 Android Gradle 构 ...
- HDU 1532 --&&-- POJ1273 dinic 算法
学长的代码#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...
- 混合开发的大趋势之 一个Android程序员眼中的 React.js 箭头函数,const, PropTypes
转载请注明出处:王亟亟的大牛之路 昨天写了篇React.js的开头之作,讲了讲块级作用域和let,先安利:https://github.com/ddwhan0123/Useful-Open-Sourc ...
- java中规范语句
1. 直接常量:A=a,a是数字,是定死的数字,简单说是常数 符号常量:A=a,a是定死的符号,
- tomcat集群基于Nginx——共享同一个应用
1.首先准备两个tomcat,也可以一个复制两个.和一个Nginx tomcat官方下载连接——安装版&绿色版 Nginx官网下载链接:http://nginx.org/download/ 博 ...
- localhost不能访问127.0.0.1可以访问的原因以及解决办法
今天在调试程序的时候,出现了一个奇怪问题,localhost不能访问但127.0.0.1可以访问? localhost与127.0.0.1的概念和工作原理之不同 要比较两个东西有什么不同,首先要弄清两 ...
- angular custom Element 自定义web component
angular 自定义web组件: 首先创建一个名为myCustom的组件. 引入app.module: ... import {customComponent} from ' ./myCustom. ...
- [Kafka] - Kafka Java Producer代码实现
根据业务需要可以使用Kafka提供的Java Producer API进行产生数据,并将产生的数据发送到Kafka对应Topic的对应分区中,入口类为:Producer Kafka的Producer ...
- 教你10步闯进google play排行榜前列
1.正视最高榜单的价值 我们需要了解排名对你的游戏的价值,进入前20名你的游戏获得每日至少1万5千的安装量,而前10名获得至少2万5千的安装量.通过奖励性广告网络而获得这些流量需要你每日支付至 ...
- iOS自动化探索(四)自动化测试框架pytest - 安装和使用
自动化测试框架 - pytest pytest是Python最流行的单元测试框架之一, 帮助更便捷的编写测试脚本, 并支持多种功能复杂的测试场景, 能用来做app测试也能用作函数测试 官方文档: ht ...