659. Split Array into Consecutive Subsequences
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, 5
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]
Approach #1: C++.
class Solution {
public:
bool isPossible(vector<int>& nums) {
int pre = nums[0] - 1;
int a1 = 0, a2 = 0, a3 = 0;
for (int i = 0; i < nums.size(); ) {
int j = i;
while (j+1 < nums.size() && nums[j+1] == nums[j]) ++j;
int cnt = j - i + 1;
int cur = nums[i];
if (cur != pre + 1) {
if (a1 != 0 || a2 != 0) return false;
a3 = 0;
a1 += cnt;
} else {
int b1 = 0, b2 = 0, b3 = 0;
if (a1 > cnt) return false;
b2 += a1, cnt -= a1, a1 = 0;
if (a2 > cnt) return false;
b3 += a2, cnt -= a2, a2 = 0;
b3 += min(a3, cnt), cnt -= min(cnt, a3);
a1 = cnt;
a2 = b2;
a3 = b3;
}
pre = cur;
i = j + 1;
}
return a1 == 0 && a2 == 0;
}
};
Analysis:
In this problem we use a1, a2, a3 represent the number of the subsequences with the length of 1, 2, 3.
cnt represent the number of same elements in this loop.
pre represent the number in the last time loop we force on (nums[i-1]).
first : we should judge if the array is consequent with the pre number and cur number. if so, we continue the next step, otherwise, we should judge if a1 and a2 equal to 0.
second : we should put the cur number in to the previous subsequences with the length of 1 or 2. if at this loop the same numbers (cnt) smaller than a1 or a2, this means that in the next loop we will have subsequences' length less than 3, so we should return false; otherwise, we update the value of a1, a2 and a3.
finlly : we judge if a1 == 0 and a2 == 0.
Approach #2: Java.
class Solution {
public boolean isPossible(int[] nums) {
int pre = Integer.MIN_VALUE, p1 = 0, p2 = 0, p3 = 0;
int cur = 0, cnt = 0, c1 = 0, c2 = 0, c3 = 0;
for (int i = 0; i < nums.length; pre = cur, p1 = c1, p2 = c2, p3 = c3) {
for (cur = nums[i], cnt = 0; i < nums.length && cur == nums[i]; cnt++, i++);
if (cur != pre + 1) {
if (p1 != 0 || p2 != 0) return false;
c1 = cnt; c2 = 0; c3 = 0;
} else {
if (cnt < p1 + p2) return false;
c1 = Math.max(0, cnt - (p1 + p2 + p3));
c2 = p1;
c3 = p2 + Math.min(p3, cnt - (p1 + p2));
}
}
return (p1 == 0 && p2 == 0);
}
}
659. Split Array into Consecutive Subsequences的更多相关文章
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- [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 ...
- [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 ...
- 【leetcode】659. Split Array into Consecutive Subsequences
题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为 ...
- 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] 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 ...
随机推荐
- JavaScript,Dom,jQuery
JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript 语言的规则编写相应代码,浏览器可以解释出相应的处理. 注 ...
- Spring单例Bean和线程安全
Spring的bean默认都是单例的,这些单例Bean在多线程程序下如何保证线程安全呢?例如对于Web应用来说,Web容器对于每个用户请求都创建一个单独的Sevlet线程来处理请求,引入Spring框 ...
- 开发工具:sublime text3安装Vue语法高亮插件
一.将插件克隆到Sublime的packages目录 1.下载并解压插件(或直接git命令clone),得到vue-syntax-highlight-master文件夹 插件地址:https://gi ...
- 将Windows下磁盘出现黑色为分配区域变成绿色区域
在windows下不知什么原因, 有一块磁盘空间F盘就变成了黑色为分配区域. 黑色区域无法用来安装双系统, 网上查阅资料后, 找到了如何将他重新变回绿色区域的2个方法(方法二是自己无意操作成功的). ...
- MessageBox如何输出整数
int cx=10;CString s;s.Format(_T("整数是:%d"),cx);MessageBox(s);
- Nginx简单入门教学,包学包会,让你不再依赖伪大神!
这篇教程简单介绍了 nginx 并且讲解了一些 nginx 可以解决的简单任务.这里,我们假设 nginx 已经安装在读者的机器上.如果没有,可以看一下如何安装 nginx.这篇教程主要讲解的是如果启 ...
- Javascript面向对象(二):构造函数的继承
这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...
- python的文件锁使用
python的文件锁目前使用的是fcntl这个库,它实际上为 Unix上的ioctl,flock和fcntl 函数提供了一个接口. 1.fcntl库的简单使用 import fcntl import ...
- hadoop-eclipse-plugin-2.6.0-cdh5.4.0 插件编译
1.JDK配置 1) 安装jdk 2) 配置环境变量 JAVA_HOME.CLASSPATH.PATH等设置 2.Eclipse 1).下载eclipse-jee-juno-SR2.rar 2).解压 ...
- sql中IN的用法
1.和where配合使用 IN操作符允许我们在where的子句中规定多个值 SELECT * FROM Persons WHERE LastName IN ('Adams','Carter') 这句 ...